STRING HANDLING FUNCTIONS
In ‘C’ language the group of characters, digits and symbols enclosed with in double quotation marks are called String. The string is always declared as character arrays. In other words character arrays are called Strings. To manipulate text such as words and sentences normally strings are used. Every string is terminated with ‘\0’ (NULL) character. The NULL character is a byte with all bits at logic 0.Hence its decimal value is Zero.
For Example:
char ch[]={ ‘W’, ‘E’ , ‘L’ , ‘C’ , ‘O’ , ‘M’ ,
‘E’ , ‘\0’ };
Note: It is not compulsory to write ‘\0’ in
string.
The compiler automatically places ‘\0’ at the end of
the character array (or) string.
‘C’ library supports a large number of String-handling functions that can be used to carry out many of the string manipulations.Following are the most commonly used string handling functions.
FUNCTION ACTION strlen () Finds the length of a string. strcpy () Copies one string over another. strcmp () Compares two strings. strcat () Concatenates two strings
strlen (): This function counts the number of characters
in a given string.
Syntax: strlen(string);
Example:
void main() { char ch[20]; int i; clrscr(); printf("Enter the string"); gets(ch); i=strlen(ch); printf("Length of given string is %d", i); getch(); }
OUTPUT: Enter the string: Hello
Length of given
string is 5.
strcpy (): This function copies the
contents of one string to another.
Syntax: strcpy (string2,
string1);
Where string1-<source string
string2-<Destination string
string1 is copied to string2.
Example:
void main() { char str1[20],str2[20]; clrscr(); printf("Enter the string1"); gets(str1); strcpy(str2,str1); printf("First string is %s", str1); printf("second string is %s", str2); getch(); }
OUTPUT: Enter the string1 : hello
First string
is hello.
Second string is hello.
strcmp():
This function compares two strings identified by the arguments
and has a value 0 if they are equal.If they are not it has the
numeric difference between the first non matching character in
the strings.
Syntax: strcmp(string1 ,string2);
String1
and string2 may be string variables or string constants.
Example: strcmp(name1,name2);
Strcmp(name1, "abc");
Strcmp("Rom", "Ram");
--< Our major concern is to
determine whether strings are equal;if not which is
alphabetically above.
The value of mismatch is rarely
important.
Example: strcmp("their", "there");
The
above example will return -9 which is numric difference between
ASCII "i" and ASCII "r".
i.e., "i" minus "r" in ASCII code
is -9.
If the value is negative ,string1 is alphabetically
above string2.
Example program:
void main() { char str1[20],str2[20]; int k; clrscr(); printf("Enter the string1"); gets(str1); printf("Enter the string2"); gets(str2); k=strcmp(str1,str2); if(k==0) printf("Two Strings are equal"); else if(k<0) printf("string1< string2"); else printf("string1 > string2"); getch(); }
strcat():This function joins two strings together.
Syntax: strcat(string1,string2);
string1 and string2 are
character arrays.
When the function "strcat" is executed, string2 is appended to string1.It does so by removing the null character at the end of the string1 and placing string2 from there. The string at string2 remains unchanged. Example:
void main() { char str1[20]= "WELCOME TO"; char str2[20]= "C LANG"; clrscr(); strcat(st1,st2); printf("string1 is %s", str1); printf("string2 is %s", str2); getch(); }
OUTPUT: string1 is WELCOME TO C LANG.
String2 is
C LANG.
Some more string handling function:
1. strncpy (): This function performs the same task
as strcpy().The only difference between them is that the former
function copies specified length of characters from source to
destination.
Syntax: strncpy (destination, source, n);
2. stricmp (): This function compares two strings. The
characters of the strings may be in lower case or upper case the
function doesn’t discriminates between them.i.e., This function
compares two strings without case. If the strings are same it
returns Zero otherwise non-zero value.
Syntax:
stricmp(string1,string2);
3. strlwr (): This
function is used to convert any string to a lower case. When you
are passing any upper case string to this function it converts
into lower case.
Syntax: strlwr (string);
4. strupr
(): This function converts lowercase strings to upper case.
Syntax: strupr (string);
5. strdup (): This
function is used for duplicating a given string at the allocated
memory which is pointed by a pointer variable.
Syntax:
str2=strdup (str1);
6. strrev (): This function
simply reverses the given string.
Syntax: strrev (string);
7. strset (): This function replaces every character
of a string with the symbol given by the programmer,i.e., the
elements of strings are replaced with arguments given by the
programmer.
Syntax: strset(string, symbol);