Built-In Functions of String:
The “string.h” header file contains the functions that are used to process strings. The commonly used functions of this header file are given below.
The “strlen” Function:
The “strlen” stands for string length. This function is used to find the length of a string. It counts the total number of characters including spaces. Null character is excluded.
Its syntax is:
int strlen (string);
Where string represents the string whose number of characters are to be counted. It may he a constant string or a string type variable. It returns integer type value.
The “strcmp” Function:
The “strcmp” stands for string compare. This function is used to compare two strings. It compares first string with the second string character by character.
Its syntax is:
int strcmp(stringl, string2);
Where
string 1: represents the first string. It may be a string constant or a string type variable.
string 2: represents the second string. It may be a string constant or string type variable.
The “strnemp” Function:
This function is similar to the “strnemp” function but it compares the specified number of characters of the two strings.
Its syntax is:
int strnemp(string 1, string 2, n);
Where
String 1 represents the first string. It may be a string constant or a string type variable.
String 2 represents the second string. It may be a string constant or a string type variable.
n represents a specified number of characters that are to be compared. It may he an integer constant or integer type variable.
The “strcpy” Function:
The “strcpy” stands for string copy. It is used to copy the contents of one string to another string variable including the null character (‘\0’).
Its syntax is:
Strcpy (string l, string 2);
Where
String 1 represents the first string, it must be a string type variable because the contents of string 2 are to be copied into it.
String 2 represents the second string. It may be a string constant or string type variable.
For example, to Copy the string “I love my School” into the string variable “sch”, the statement is written as:
strcpy (sch, “I love my School”);
The “strncpy” Function:
It is similar to “strcpy” function but it is used to copy a specified number of characters from one string to another string variable.
Its syntax is:
strncpy (string 1, string 2, n);
Where
String 1 represents the first string. It must be a string type variable because the first “n” characters of string2 are to be copied into it.
String 2 represents the second string. It may be a string constant or a string type variable. The first “n” characters of this string are copied into the string 1, represents the number of characters of string2 that are to be copied into string 1. It may be an integer constant or an integer type variable.
The “strcat” Function:
The “strcat” stands for string concatenate. It is used to append or combine the contents of one string to another string variable including the null character. Its syntax is:
strcat (string 1 , string 2);
Where
string 1 represents the first string. It must be a string type variable because the contents of string2 are to be added at the end of this string.
string 2 represents the second string. It may be a string constant or a string type variable. The contents of this string are added at the end of the string 1.
The “strncat” Function:
It is similar to “strcat” function hut it is used to append a specified number of characters of one string to another string variable. Its syntax is:
strncat (string l, string2, n);
Where
String 1 represents the first string. It must be a string type variable because the specified number of characters of string2 is appended at the end of this string.
String 2 represents the second string. It may be a string constant or a string type variable. The specified characters of this string are appended at the end of siring 1 represents the number of characters of string 2 that have to be appended at the end of string 1. It may he an integer value or an integer type variable.
comment closed