String Functions in C
This article is about the various string functions available in C programming and the usage of it in our programs.
String functions in C programming
C supports a wide range of functions that manipulates and perform operations on the string. The string handling functions are present in the standard library “string.h” which should be defined in the header file.
strcpy(s1, s2);
The strcpy() function copies string s2 into string s1. Here the size of the destination string should be enough to store the source string.
**char *strcpy(char *destination, const char *source)**
Example
[maxbutton id=”1″ url=”https://coderseditor.com/?id=238″ ]
#include <stdio.h> #include <string.h> int main() { char str1[20] = "programming"; char str2[20]; // copying str1 to str2 strcpy(str2, str1); printf("str2: %s\n", str2 ); return 0; }
Output
str2: programming
strcat(s1, s2)
Concatenates string s2 onto the end of string s1. This function simply joins two strings. To use a strcat() function the size of the destination string should be large enough to store the resultant string so as to avoid errors.
**char *strcat(char *destination, const char *source)**
Example
[maxbutton id=”1″ url=”https://coderseditor.com/?id=239″ ]
#include <stdio.h> #include <string.h> int main() { char str1[15] = "Hai!"; char str2[15] = "Hello"; strcat( str1, str2); printf("str1: %s\n", str1 ); return 0; }
Output
str1: Hai!Hello
strlen(s1)
The strlen() function calculates the length of a given string. This function doesn’t count the null character \0 while calculating the length.
ExampleExample
[maxbutton id=”1″ url=”https://coderseditor.com/?id=240″ ]
#include <stdio.h> #include <string.h> int main() { char str1[10] = "Program"; len = strlen(str1); printf("Length of string1: %d\n", len ); return 0; }
Output
Length of string1: 7
strcmp(s1, s2)
The strcmp() compares two strings character and returns 0 if s1 and s2 are the same; negative value if s1<s2; positive value if s1>s2.
Example
[maxbutton id=”1″ url=”https://coderseditor.com/?id=240″ ]
#include <stdio.h> #include <string.h> int main() { char str1[7] = "xyz", char str2[7] = "xYz", char str3[7] = "xyz"; int result; // comparing strings str1 and str2 result = strcmp(str1, str2); printf("strcmp(str1, str2) = %d\n", result); // comparing strings str1 and str3 result = strcmp(str1, str3); printf("strcmp(str1, str3) = %d\n", result); return 0; }
Output
strcmp(str1, str2) = 23
strcmp(str1, str3) = 0
strchr(s1, ch)
Returns a pointer to the first occurrence of character ch in string s1.
Example
#include <stdio.h>
#include <string.h>
int main () {
const char str[] = "www.google.com";
const char ch = '.';
char *ans;
ans = strchr(str, ch);
printf("String after |%c| is - |%s|\n", ch, ans);
return(0);
}
Output
String after |.| is - |.google.com|
strstr(s1, s2)
Returns a pointer to the first occurrence of string s2 in string s1.
Example
#include <stdio.h>
#include <string.h>
int main() {
char s1 [] = "My name is preethi";
printf ("Returned String 1: %s\n", strstr (s1, "name"));
}
Output
Returned String 1: name is preethi
strlwr()
The strlwr() function converts string to lowercase.
**char strlwr(char str);
Example
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "BEAUTIFUL";
// converting the given string into lowercase.
printf("%s\n",strlwr (str));
return 0;
}
Output
beautiful
strupr()
The strupr() function converts string to uppercase.
Example
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "awesome";
// converting the given string into uppercase.
printf("%s\n",strupr (str));
return 0;
}
Output
AWESOME