C String Functions in C Language
Categories: C language
There are many important string functions defined in "string.h" library.
No.FunctionDescription
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 second string. If both strings are same, 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 null character '\0'.
#include<stdio.h>
#include <string.h>
int main(){
char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
printf("Length of string is: %d",strlen(ch));
return 0;
}
Output:
Length of string is: 10
C Copy String: strcpy()
The strcpy(destination, source) function copies the source string in destination.
#include<stdio.h>
#include <string.h>
int main(){
char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
char ch2[20];
strcpy(ch2,ch);
printf("Value of second string is: %s",ch2);
return 0;
}
Output:
Value of second string is: javatpoint
C String Concatenation: strcat()
The strcat(first_string, second_string) function concatenates two strings and result is returned to first_string.
#include<stdio.h>
#include <string.h>
int main(){
char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};
char ch2[10]={'c', '\0'};
strcat(ch,ch2);
printf("Value of first string is: %s",ch);
return 0;
}
Output:
Value of first string is: helloc