Monday 12 June 2023

String Library Functions in C

What are string library functions?

There are several or some library functions used to manipulate or operate strings. The prototypes for these functions or task are in header file string.h. We’ll discuss or talk over some of them below-

string, library functions, c language, c program
String library function in C language image-1

1.1   strlen()

This function or task returns the length or distance of the string i.e. the number of characters or nature in the string excluding or not taking someone the terminating null character. It accepts or receives a single argument or difference of opinion, which is pointer to the first nature or character of the string. For example strlen(“computer”) returns the value 8. Similarly is s1 is an array that contains the name “hyderabad” then strlen(s1) returns the value 9.

1.2   strcmp()

This function or task is used for comparison or contrast of two strings. The comparison or contrast stops when either the end of string is reached or extend or the analogous or corresponding characters in the two strings are not same. The non-zero value returned or go back on mismatch is the difference of the ASCII values of the non-matching or un-matching characters of the two strings-

Strcmp(s1, s2) return a value-

<0 when s1<s2

=0 when s1==s2

>0 when s1>s2

Generally or normally we don’t use the exact or precise non-zero value returned in case of mismatch or discrepancy. We only need to know its sign or indication to compare the alphabetical place of the two strings. We can use this task to sort the strings alphabetically.

1.3   strcpy():

This task is used for copying on string to another string. Strcpy(str1, str2) duplicate str2 to str1. Here str2 is the source string and str1 is something is going or destination string. If str2= “computer” then this function duplicate “computer” into str1. This function takes pointers to two strings as arguments or logic and returns or goes back the pointer to first string.

1.4   strcat()

This task is used for concatenation or series of two strings. If first string is “abcd” and second string is “efgh” then after using this task the first string becomes “abcdefgh”.

Strcat(str1, str2); /*concatenates or series str2 at the end of str1 */

The null character or nature from the first string is removed, and the second, string is added or enhancing quality at the end of first string. The second string remains unchanged.

This task takes pointer to two strings as arguments or logic and returns a pointer to the first (concatenated or series) string.


Computer stuff kit tricks of Topics 57.