String Functions in C Language

The header file <string.h> contains some string manipulation functions. The following is a list of the common string managing functions in C.

Strlen( )

The strlen function returns the length of a string. It takes the string name as an argument.

The syntax is as follows:
n = strlen(str);
where str is the name of the string and n is the length of the string, returned by the strlen function.

Strcpy ( )

In C, you cannot simply assign one character array to another. You have to copy element by element.

The string library <string.h> 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.

Strcmp ( )

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.

Strcat ( )

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.

Strlwr( )

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.

Strrev ( )

The strrev funtion reverses the given string.

The syntax is as follows:
strrev(str);
where string str will be reversed.

Strspn ( )

The strspn function returns the position of the string, where first string mismatches with the second string.

The syntax is as follows:
n = strspn (first, second);

where first and second are two strings to be compared, n is the number of character from which first string does not match with the second string.

OTHER STRING FUNCTIONS

strncpy function

The strncpy function same as strcpy. It copies characters of one string to another string up to the specified length.

The syntax is as follows:
strncpy(str1, str2, 10);
where str1 and str2 are two strings. The 10 characters of string str2 are copied onto string str1.

stricmp function

The stricmp function is same as strcmp, except it compares two strings ignoring the case (lower and upper case).

The syntax is as follows:
n = stricmp(str1, str2);

strncmp function

The strncmp function is same as strcmp, except it compares two strings up to a specified length.

The syntax is as follows:
n = strncmp(str1, str2, 10);
where 10 characters of str1 and str2 are compared and n is returned value of differed characters.

strchr function

The strchr funtion takes two arguments (the string and the character whose address is to be specified) and returns the address of first occurrence of the character in the given string.

The syntax is as follows:
cp = strchr (str, c);
where str is string and c is character and cp is character pointer.

strset function

The strset funtion replaces the string with the given character. It takes two arguments the string and the character.

The syntax is as follows:
strset (first, ch);
where string first will be replaced by character ch.

strchr function

The strchr function takes two arguments (the string and the character whose address is to be specified) and returns the address of first occurrence of the character in the given string.

The syntax is as follows:
cp = strchr (str, c);
where str is string and c is character and cp is character pointer.

strncat function

The strncat function is the same as strcat, except that it appends upto specified length.

The syntax is as follows:
strncat(str1, str2,10);
where 10 character of the str2 string is added into str1 string.

strupr function

The strupr function converts lower case characters of the string to upper case characters.

The syntax is as follows:
strupr(str1);
where str1 is string to be converted into upper case characters.

strstr function

The strstr function takes two arguments address of the string and second string as inputs. And returns the address from where the second string starts in the first string.

The syntax is as follows:
cp = strstr (first, second);
where first and second are two strings, cp is character pointer.

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: