String Handling in C PDF
String Handling in C PDF
String Handling in C PDF
What is a string?
A string is combination of characters.
Any set or sequence of characters defined within double quotation symbols is a constant string.
The string in C programming language is actually a one-dimensional array of characters which is
terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise
the string followed by a null.
Initializing Strings
The initialization of a string must the following form which is simpler to the one dimension array
char month1[ ]={‘j’,’a’,’n’,’u’,’a’,’r’,’y’};
1. strlen( ) function:
This function counts and returns the number of characters in a particular string. The length always
does not include a null character. The syntax of strlen() is as follows:
n=strlen(string);
Where n is the integer variable which receives the value of length of the string.
The following program shows to find the length of the string using strlen() function
/*writr a c program to find the length of the string using strlen() function*/
#include < stdio.h >
include < string.h >
void main( )
{
char name[100];
int length;
printf(”Enter the string”);
gets(name);
length=strlen(name);
printf(”\nNumber of characters in the string is=%d”,length);
}
2. strcat( ) function:
when you combine two strings, you add the characters of one string to the end of the other string. This
process is called as concatenation. The strcat() function is used to joins 2 strings together. It takes the
following form:
strcat(string1,string2)
string1 & string2 are the character arrays. When the function strcat is executed string2 is appended to
the string1. the string at string2 always remains unchanged.
//Sample program.
void main( )
{
char a[20],b[20];
clrscr();
printf("\n Enter first string : ");
gets(a);
printf("\n Enter second string : ");
gets(b);
strcat(a," ");
strcat(a,b);
printf("\n Concatenated string is: %s",a);
getch( );
}
3. strcpy( ) function:
To assign the characters to a string,C does not allow you directly as in the statement name=Robert;
Instead use the strcpy() function found in most compilers the syntax of the function is illustrated
below.
strcpy(string1,string2);
//sample program
void main( )
{
char a[50],b[50];
clrscr();
printf("\n Enter a string: ");
gets(a);
strcpy(b,a);
printf("\n String after copying: ");
puts(b);
printf("\n String before copying: ");
puts(a);
getch( );
}
4. strcmp( ) function:
In c,you cannot directly compare the value of 2 strings in a condition like if(string1==string2) Most
libraries however contain the function called strcmp(),which returns a zero if 2 strings are equal, or a
non zero number if the strings are not the same. The syntax of strcmp() is given below:
strcmp(string1,string2)
void main( )
{
char str1[ ] = "fresh" ;
char str2[ ] = "refresh" ;
int i, j, k ;
i = strcmp ( str1, "fresh" ) ; // 0
j = strcmp ( str1, str2 ) ; // -1
k = strcmp ( str1, "f" ) ; // 1
printf ( "\n%d %d %d", i, j, k ) ;
getch( );
}
5. strrev( ) function:
This function reverses the characters in a particular string. The syntax of the function strrev is
illustrated below
strrev(string);
//sample program
void main( )
{
char a[50];
clrscr();
printf("\n Enter a string: ");
gets(a);
printf("\n String before reversed:%s",a);
printf("\n String after reversing:%s",strrev(a));
getch( );
}