C String Functions
There are many important string functions defined in "string.h" library.
S.No. | Function | Description |
---|---|---|
1. | strlen(string_name) | returns the length of string name. |
2. | strcpy(destination, source) | copies the contents of source string to destination string. |
3. | strcat(first_string, second_string) | concats or joins first string with second string. The result of the string is stored in first string. |
4. | strcmp(first_string, second_string) | compares the first string with the second string. If both strings are smae, then it returns 0. |
5. | strrev(string) | returns reverse string. |
6. | strlwr(string) | returns string characters in lowercase. |
7. | strupr(string) | returns string characters in uppercase. |
C String Length : strlen() Function
The strlen() function returns the length of the given string. It doesn't count the null character '\0'.
#include <stdio.h>
#include <string.h>
int main()
{
char ch[20] = {'D', 'e', 'e', 'p', 'a', 'k', '\0'};
printf("Length of string is : %d\n", strlen(ch));
return 0;
}
Output :
Length of string is : 6
C Copy String : strcpy() Function
The strcpy(destination, source) function copies the source string in destination.
#include <stdio.h>
#include <string.h>
int main(){
{
char ch1[20] = {'D', 'e', 'e', 'p', 'a', 'k', '\0'};
char ch2[20];
strcpy(ch2, ch1);
printf("Value of second string is : %s\n", ch2);
return 0;
}
Output :
Value of second string is : Deepak
C String Concatenation : strcat() Function
The strcat(first_string, second) function concatenates two strings and result is returned to first_string.
#include <stdio.h>
#include <string.h>
int main()
{
char ch3[10] = {'D', 'e', 'e', 'p', '\0'};
char ch4[10] = {'a', 'k', '\0'};
strcat(ch3, ch4);
printf("Value of first string is : %s\n", ch3);
return 0;
}
Output :
Value of first string is : Deepak
C Compare String : strcmp() Function
The strcmp(first_string, second_string) function compares two strings and returns 0 if both strings are equal.
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20], str2[20];
printf("Enter first string : ");
gets(str1);
printf("Enter second string : ");
gets(str2);
if (strcmp(str1, str2) == 0)
printf("Strings are equal.\n");
else
printf("Strings are not equal.\n");
return 0;
}
Output :
Enter first string : Hello
Enter second string : hello
Strings are not equal.
Enter first string : Hello
Enter second string : Hello
Strings are equal.
C Reverse String : strrev() Function
The strrev(string) function returns reverse of the given string.
#include <stdio.h>
#include <string.h>
int main()
{
char str[20];
printf("Enter string : ");
gets(str);
printf("Reverse of string is : %s", strrev(str));
return 0;
}
Output :
Enter string : Deepak
Reverse of string is : kapeeD
C String Lowercase : strlwr() Function
The strlwr(string) function returns string characters in lowercase.
#include <stdio.h>
#include <string.h>
int main()
{
char str[20];
printf("Enter string : ");
gets(str);
printf("Lower String : %s\n", strlwr(str));
return 0;
}
Output :
Enter string : DeEpAk
Lower String : deepak
C String Uppercase : strupr() Function
The strupr(string) function returns string characters in uppercase.
#include <stdio.h>
#include <string.h>
int main()
{
char str[20];
printf("Enter string : ");
gets(str);
printf("Upper String : %s\n", strupr(str));
return 0;
}
Output :
Enter string : DeEpAk
Upper String : DEEPAK
String strstr() Parameters
The strstr() function returns pointer to the first occurence of the matched string in the given string. It is used to return substring from first match till the last character.
char *strstr(const char *string, const char *match)
string : It represents the full string from where substring will be searched.
match : It represents the substring to be searched in the full string.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100] = "These are C notes by Deepak Chandra Sharma";
char *sub;
sub = strstr(str, "Deepak");
printf("Substring is : %s", sub);
return 0;
}
Output :
Substring is : Deepak Chandra Sharma
Comments
Post a Comment