Strings Handling Functions
As we learnt that C has provided many built-in functions to work with strings, we will continue to know one by one with example programs.
Strlen Function
The strlen function returns the length of a string. It takes the string name as argument.
The syntax is as follows:
n = strlen (str);
where str is name of the string and n is the length of the string, returned by strlen function.
/* Program to illustrate the strlen function to determine the length of a string */
#include
#include
main()
{
char name[80];
int length;
printf(“Enter your name: ”);
gets(name);
length = strlen(name);
printf(“Your name has %d characters\n”, length);
}
Strcpy Function
In C, you cannot simply assign one character array to another. You have to copy element by element. The string library contains a function called strcpy for this purpose. The strcpy function is used to copy one string to another.
The syntax is as follows:
strcpy(str1, str2);
where str1, str2 are two strings. The content of string str2 is copied on to string str1.
/* Program to illustrate strcpy function*/
#include
#include
main()
{
char first[80], second[80];
printf(“Enter a string: ”);
gets(first);
strcpy(second, first);
printf(“\n First string is : %s, and second string is: %s\n”, first, second);
}
Strcmp Function
The strcmp function in the string library function which compares two strings, character by character and stops comparison when there is a difference in the ASCII value or the end of any one string and returns ASCII difference of the characters that is integer. If the return value zero means the two strings are equal, a negative value means that first is less than second, and a positive value means first is greater than second.
The syntax is as follows:
n = strcmp(str1, str2);
where str1 and str2 are two strings to be compared and n is returned value of differed characters.
/* The following program uses the strcmp function to compare two strings. */
#include
#include
main()
{
char first[80], second[80];
int value;
printf(“Enter a string: “);
gets(first);
printf(“Enter another string: “);
gets(second);
value = strcmp(first,second);
if(value==0)
printf(“strings are same”);
else
{
if(value==1)
printf(“second string is big”);
else
{
if(value==-1)
printf(“first string is big”);
}
}
}
strcat Function
The strcat function is used to join one string to another. It takes two strings as arguments; the characters of the second string will be appended to the first string.
The syntax is as follows:
strcat(str1, str2);
where str1 and str2 are two string arguments, string str2 is appended to string str1.
/* Program for string concatenation*/
#include
#include
main()
{
char first[80], second[80];
printf(“Enter a string:”);
gets(first);
printf(“Enter another string: ”);
gets(second);
strcat(first, second);
printf(“\nThe two strings joined together: %s\n”, first);
}
strlwr Function
The strlwr function converts upper case characters of string to lower case characters.
The syntax is as follows:
strlwr(str1);
where str1 is string to be converted into lower case characters.
/* Program that converts input string to lower case characters */
#include
#include
main()
{
char first[80];
printf(“Enter a string: “);
gets(first);
printf(“Lower case of the string is %s”, strlwr(first));
}
strrev Function
The strrev funtion reverses the given string.
The syntax is as follows:
strrev(str);
where string str will be reversed.
/* Program to reverse a given string */
#include
#include
main()
{
char first[80];
printf(“Enter a string:”);
gets(first);
printf(“\n Reverse of the given string is : %s ”, strrev(first));
}