Skip to main content

Describe the different types of string handling functions in C language with example


Answer:

Different type of string handling function:

Function Name

Description
strcat()
Concatenates two strings.
strcmp()

Compares two strings.
strcpy()

Copies a string from source to destination.
strlen()

Returns the length of a srting.
strlwr()
Return upper case letter to lower case.
strupr()

Return lower case letter to upper case.
Strrev()

Reverse of a string.

Strcat() function:
The strcat() function joins two strings together.



General form:
                        strcat(string1, string2)
Where string1 and string2 are character arrays. When the functions strcat() is executed, string2 is appended to string1. It does so by removing the null character at the end of string1 and placing string2 from there. The string at string2 remains unchanged.

Example:
char string1[]={“VERY ”}, string2[]={“GOOD”};
    puts(strcat(string1,string2));

Output:
            VERY GOOD

strcmp() function:
The strcmp() function compares two strings identified by the arguments and has a value 0 if they are equal. If they are not, it has the numberic difference between the first nonmatching characters in the strings.

General form:
strcmp(string1, string2);
Where string1 and string2 are character arrays.

Examples:
strcmp(name1, name2);
strcmp(“Rom”, “Ram”);

strcpy() function:
The strcpy() function works almost like a string-assignment operator.

General form:
strcpy(string1, string2);
where string1 and string2 are character arrays.
It assigns the contents of string2 to string1.

Example:
strcpy(city, “DHAKA”);
will assign the string “DHAKA” to the string variable city.

strlen() function:
This funnction counts and returns the number of characters in a string.

General form:
                        strlen(string);
Where string is a character array.
It returns the value of the length of the string. The counting ends at the first null character.

Example:
char fname[30]={“DHAKA”};
int length=strlen(fname);
This would set length to 5.

strlwr() function:
This function converts all characters in a string from uppercase to lowercase.
General form:
     strlwr(string);
Where string is a character array.
Example:
     char string[30]={“DHAKA”};
     puts(strlwr(string));
Output:
     dhaka

strupr() function:
This function converts all characters in a string from lowercase to uppercase.
General form:
     strupr(string);
Where string is a character array.
Example:
     char string[30]={“dhaka”};
     puts(strupr(string));
Output:
     DHAKA

strrev() function:
This function reverses the characters in a particular string.
General form:
            strrev(string);
Where string is a character array.
Example:
char string[30]={“DHAKA”};
puts(strrev(string));
Output:

            AKAHD