String Functions in C

String Functions in C
Techiio-author
Written by Shuvhojit DebMarch 1, 2022
10 min read
C
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this blog, you'll get to know about String Functions in C.

What are C String Functions?

String capacities structure the foundation of any programming language as it lets you know how a specific language is taken care of with strings. Taking care of strings implies that your programming language should be adequately effective to adjust the string that you have and play out the expected changes. Numerous C string capacities are made to make your string taken care of more straightforward as in those cases you would have no need to code except for simply calling them and implementing them in your piece of code.

There are two sorts of capacities, the first is the library capacities and the second is the exclusively based capacities. In the last option, you have the opportunity to make your capacity or a strategy and use them in your code as and when you feel like it. Typically these capacities are related to exhaust enclosure. In this c string capacity article, you will figure out how to control strings utilizing C capacities alongside models like puts(), gets(), strlen(), and so on All string capacities in C are accessible in the standard library "string.h".

C language is made for creating framework-based applications which are utilized to straightforwardly connect with the equipment gadgets like portions, drivers, and so on Fundamentally for all framework-related correspondence as it is a low-level framework well-disposed programming language. C programming language is viewed as the foundation of all the programming dialects which is likewise generally called the procedural programming language, mid-level programming language, and organized programming language. It is explicit to a machine for example it is machine-subordinate and is relatively quick to run. It isn't so natural to see however as it requires the essential programming information and mentality.

Examples of String Functions in C

The string function is easy to use. Here we will discuss how to use the string function in C programming with the help of examples

1. printf():

This function is used to print the string which is present inside the double quotes (“”) of this function. It can also be used to concatenate two strings.

e.g-

#include<stdio.h>
int main()
{
printf("Name: Hardik");
}

2. gets():

This function is used to get the input string from the user.

e.g-

#include<stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
gets(name);
}

3. puts():

This is the C language-based function that is used to display the string on the console screen.

This is different from the printf() function in the regard that puts() writes the string s and a newline to stdout i.e. it is only used to display the strings whereas the printf() is used to display all kinds of outputs to stdout.

e.g-

#include<stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
gets(name);
puts(name);
}

4. char:

This function in the below example means that the string of s is declared with a size of 5 with the character data type. This comes under the declaration and definition part of the string.

e.g-

char s[5]

5. scanf():

This is another string function popularly used in the C language. It is used to read the characters till the time whitespace such as space, tab, or a newline is encountered. In essence, this is used to read a string.

e.g-

#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
}

6. strcpy(s1, s2):

This function is used to copy the contents of the string s2 into the primary string s1.

e.g-

#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int  len ;
/* copy str1 into str3 */
strcpy(str3, str1);
}

7. strcat():

This function is used to concatenate the contents of string s2 after string s1.

e.g-

#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int  len ;
strcat( str1, str2);
}

8. Strlen():

As the name suggests, this function is used to calculate the length of the string.

e.g-

#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int  len ;
len = strlen(str1);
printf("strlen(str1) :  %d\n", len );
}

9. strrev():

This function is used to return the reversal of the string provided.

e.g-

#include<stdio.h>
#include <string.h>
int main(){
char str[20];
printf("Enter string: ");
gets(str);
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
}

10. strcmp():

This function is used to compare the two strings and return 0 if both the strings are equal.

e.g-

#include<stdio.h>
#include <string.h>
int main(){
char str1[10],str2[10];
gets(str1);
gets(str2);
if(strcmp(str1,str2)==0)
printf("Strings :equal");
else
printf("Strings: not equal");
}

11. strupr():

This function is used to return the characters in the upper case.

e.g-

#include<stdio.h>
#include <string.h>
int main(){
char str[20];
gets(str);
printf("String is: %s",str);
printf("\nUpper String is: %s",strupr(str));
}

12. Strlwr():

As the name suggests, this function is used to return the characters of the string in the lower case.

e.g-

#include<stdio.h>
#include <string.h>
int main(){
char str[20];
gets(str);
printf("String is: %s",str);
printf("\nUpper String is: %s",strlwr(str));
}

13. sizeof():

This function is used to return the size of the string i.e. the characters the string is capable to hold at a stretch.

e.g-

#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Hardik";
sizeof(str1)
}

14. strchr():

This function searches for the character in a particular string.

e.g-

#include <stdio.h>
#include <string.h>
int main () {
const char str[] = "http://www.google.com";
const char ch = '.';
char *ret;
ret = strchr(str, ch);
printf("String after |%c| is - |%s|\n", ch, ret);
}

The output for the above code snippet is:

Output:- String after |.| is – |.google.com|

There are many pre-built library functions in the C programming language and it also provides you the option of creating your custom function. Even if you do not want to create a function, you can write a piece of code corresponding to your requirement, but by making use of functions you will just make your job a lot easier and more convenient.

C Language
Program
Functions of C
C String Function
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance