Google News
logo
String Handling Functions In C Language

1) strlen( ) Function :

   strlen( ) function is used to find the length of a character string. 
 
 Example:        int  n; 
             char st[20] = “Bangalore”; 
             n = strlen(st); 
 
• This will return the length of the string 9 which is assigned to an integer variable n.  
•  Note that the null character “\0‟ available at the end of a string is not counted. 

2) strcpy( ) Function :

  strcpy( ) function copies contents of one string into another string. Syntax for strcpy function is given below.

Syntax: char * strcpy (char * destination, const char * source);

Example:
strcpy ( str1, str2) – It copies contents of str2 into str1.
strcpy ( str2, str1) – It copies contents of str1 into str2.

If destination string length is less than source string, entire source string value won’t be copied into destination string.
For example, consider destination string length is 20 and source string length is 30. Then, only 20 characters from source string will be copied into destination string and remaining 10 characters won’t be copied and will be truncated.

 Example :  char  city[15];
                  strcpy(city, “BANGALORE”) ;
 
 This will assign the string “BANGALORE” to the character variable city. 


3) strcat( ) Function :

  strcat( ) function in C language concatenates two given strings. It concatenates source string at the end of destination string. Syntax for strcat( ) function is given below.

Syntax :  char * strcat ( char * destination, const char * source );

Example :
strcat ( str2, str1 ); - str1 is concatenated at the end of str2.
strcat ( str1, str2 ); - str2 is concatenated at the end of str1.

• As you know, each string in C is ended up with null character (‘\0′). 
• In strcat( ) operation, null character of destination string is overwritten by source string’s first character and null character is added at the end of new destination string which is created after strcat( ) operation.

  Program : The following program is an example of strcat() function
#include <stdio.h>
#include <string.h>
int main( )
{
   char source[ ] = “ ftl” ;
   char target[ ]= “ welcome to” ;

   printf (“\n Source string = %s”, source ) ;
   printf ( “\n Target string = %s”, target ) ;

   strcat ( target, source ) ;

   printf ( “\n Target string after strcat( ) = %s”, target ) ;
}
Output :

Source string = ftl
Target string = welcome to
Target string after strcat() = welcome to ftl

4) Strncat() function :

strncat( ) function in C language concatenates (appends) portion of one string at the end of another string.

Syntax : char * strncat ( char * destination, const char * source, size_t num );

Example :
strncat ( str2, str1, 3 ); – First 3 characters of str1 is concatenated at the end of str2.
strncat ( str1, str2, 3 ); - First 3 characters of str2 is concatenated at the end of str1.

As you know, each string in C is ended up with null character (‘\0′).

In strncat( ) operation, null character of destination string is overwritten by source string’s first character and null character is added at the end of new destination string which is created after strncat( ) operation.

  Program : The following program is an example of strncat() function
#include <stdio.h>
#include <string.h>

int main( )
{
   char source[ ] =”"  ftl” ;
   char target[ ]= “welcome to” ;

   printf ( “\n Source string = %s”, source ) ;
   printf ( “\n Target string = %s”, target ) ;

   strncat ( target, source, 3 ) ;

   printf ( "”\n Target string after strncat( ) = %s”, target ) ;
}
Output :

Source string = ftl
Target string = welcome to
Target string after strncat()= welcome to ft

5) strcmp( ) Function :

strcmp( ) function in C compares two given strings and returns zero if they are same. If length of string1 < string2, it returns < 0 value. If length of string1 > string2, it returns > 0 value.

Syntax : int strcmp ( const char * str1, const char * str2 );

strcmp( ) function is case sensitive. i.e., “A” and “a” are treated as different characters.

Example :
char city[20] = “Madras”;
char town[20] = “Mangalore”;
strcmp(city, town);

This will return an integer value “-10‟ which is the difference in the ASCII values of the first mismatching letters “D‟ and “N‟.

* Note that the integer value obtained as the difference may be assigned to an integer variable as follows:

int n;
n = strcmp(city, town);

6) strcmpi() function :

strcmpi( ) function in C is same as strcmp() function. But, strcmpi( ) function is not case sensitive. i.e., “A” and “a” are treated as same characters. Whereas, strcmp() function treats “A” and “a” as different characters.

• strcmpi() function is non standard function which may not available in standard library.

Both functions compare two given strings and returns zero if they are same.

If length of string1 < string2, it returns < 0 value. If length of string1 > string2, it returns > 0 value.

strcmp( ) function is case sensitive. i.e., “A” and “a” are treated as different characters.

Syntax : int strcmpi ( const char * str1, const char * str2 );

Example :

m=strcmpi(“ DELHI ”, “ delhi ”);  m = 0.

7) strlwr() function :

strlwr() function converts a given string into lowercase.

Syntax : char *strlwr(char *string);

strlwr() function is non standard function which may not available in standard library in C.

  Program : In this program, string ”MODIFY This String To LOwer” is converted into lower case using strlwr( ) function and result is displayed as “modify this string to lower”.
#include<stdio.h>
#include<string.h>
int main()
{
    char str[ ] = “MODIFY This String To Lower”;
    printf(“%s\n”, strlwr (str));
    return 0;
}
Output :

modify this string to lower

8) strupr() function :

strupr() function converts a given string into uppercase.

Syntax : char *strupr(char *string);

strupr() function is non standard function which may not available in standard library in C.

  Program : In this program, string ”Modify This String To Upper” is converted into uppercase using strupr( ) function and result is displayed as “MODIFY THIS STRING TO UPPER”.
#include<stdio.h>
#include<string.h>

int main()
{
    char str[ ] = “Modify This String To Upper”;

    printf(“%s\n”, strupr(str));

    return 0;
}
Output :

MODIFY THIS STRING TO UPPER

9) strrev() function :

strrev() function reverses a given string in C language.

Syntax : char *strrev(char *string);

strrev() function is non standard function which may not available in standard library in C.

Example :
char name[20]=”ftl”; then
strrev(name)= ltf

  Program : In below program, string “Hello” is reversed using strrev( ) function and output is displayed as “olleH”.
#include<stdio.h>
#include<string.h>

int main()
{
   char name[30] = “Hello”;

   printf(“String before strrev( ) : %s\n”, name);

   printf(“String after strrev( )  : %s”, strrev(name));

   return 0;
}
Output :

String before strrev( ) : Hello
String after strrev( ) : olleH

10) strchr() function :

strchr() function returns pointer to the first occurrence of the character in a given string.

Syntax : char *strchr(const char *str, int character);

  Program : In this program, strchr( ) function is used to locate first occurrence of the character ‘i’ in the string ”This is a string ”. Character ‘i’ is located at position 3 and pointer is returned at first occurrence of the character ‘i’.
#include <stdio.h>
#include <string.h>
int main ()
{
  char string[25] =”This is a string “;
  char *p;
  p = strchr (string,'i');

  printf (“Character i is found at position %d\n”,p-string+1);
  printf (“First occurrence of character \”i\” in \”%s\” is” \” \”%s\””,string, p);

   return 0;
}
Output :

Character i is found at position 3
First occurrence of character “i” in “This is a string” is “is is a string”

11) strstr() function :

strstr( ) function returns pointer to the first occurrence of the string in a given string.

Syntax : char *strstr(const char *str1, const char *str2);

  Program : In this program, strstr( ) function is used to locate first occurrence of the string “test” in the string ”This is a test string for testing”. Pointer is returned at first occurrence of the string “test”.
#include <stdio.h>
#include <string.h>
int main( )
{
  char string[55] =”This is a test string for testing”;
  char *p;
  p = strstr (string, ”test”);
  if(p)
  {
    printf(“string found\n” );
    printf (“First occurrence of string \”test\” in \”%s\” is”\” \”%s\””,string, p);
  }
  else printf(“string not found\n” );
   return 0;
}
Output :

String found
First occurrence of ”test” in “this is a test string for testing” is “testing string for testing”

12) atoi() function :

It converts string-value to numeric-value and it converts a numeric-string value to equivalent integer-value.

Syntax : int atoi(string);

Example :
printf(“output=%d”, atoi(“123”)+atoi(“234”));
This printf() will print 357

13) atol() function :

converts a long int string value to equivalent long integer value.

Syntax : long int atol(string);

Example :
printf(“output=%d”, atol(“486384”)-atol(“112233”));
This statement will print 374151

14) atof() function :

converts a floating point text format value to double value.

Syntax : int atoi(string);

Example :
printf(“%1f”,atof(“3.1412”)*5*5);
This statement will print 78.530000

15) itoa(),ltoa(),ultoa() :

These functions converts a given number(int/long int/unsigned long int) to equivalent text format based on the given numbering system radix value.

These functions take three arguments, the numeric value, target string address in which the value to be stored and radix value. Finally returns the target string address, so that function-call can be used as argument/expression.

Syntax : char* itoa(int value, char *targetstringaddress, int radix );

Example :
Char temp[50];
Printf(“output=%s”, itoa(45,temp,2));

Output : 101101

  Program : Write a C program to count the number of vowels present in a sentence.
# include<stdio.h> 
# include<conio.h> 
# include<string.h> 
 main( ) 
  { 
  char  st[80], ch; 
  int  count = 0, i; 
  clrscr( ); 
  printf(“ \n Enter the sentence: \n”); 
  gets(st); 
  for( i=0; i<strlen(st); i++) 
      switch(st [i ])
        { 
   	 case  ‘A’: 
  	 case  ‘E’: 
  	 case   ‘I’: 
  	 case  ‘O’: 
  	 case  ‘U’: 
  	 case  ‘a’: 
  	 case  ‘e’: 
  	 case  ‘I’: 
  	 case  ‘o’: 
  	 case  ‘u’: 
    	   count ++; 
  	   break; 
          } 

  printf(“\n %d vowels are present in the sentence”, count); 

  getch( ); 

}  

When this program is executed, the user has to enter the sentence.

Note that gets( ) function is used to read the sentence because the string has white spaces between the words.

The vowels are counted using a switch statement in a loop.

Note that a count++ statement is given only once to execute it for the cases in the switch statement.

Output :

Enter the sentence :
This is a book
5 vowels are present in the sentence.

  Program : Write a C program to count no of lines, words and characters in a given text.
# include<stdio.h> 
# include<string.h> 
# include<conio.h> 
  main() 
    { 
   char txt[250], ch, st[30]; 
   int   ins, wds,  chs,  i;
   printf(“ \n Enter the text, type $ st end \n \n”); 
   i=0; 
   while((txt[i++]= getchar( ) ) ! =’$’);
   	i--; 
   st[ i ] = ‘\0’; 
   ins = wds = chs = 0;   

   i=0; 
   while(txt[ i ]!=’$’) 
      { 
       switch(txt[ i ]) 
    { 
     case  ‘,’: 
     case  ‘!’: 
     case  ‘\t’: 
     case  ‘ ‘: 
      { 
       wds ++;  
       chs ++;
        break; 
       } 
     case  ‘?’: 
     case  ‘.’: 
       { 
       	wds ++; 
       	chs ++;
       	break; 
       } 
     default:chs ++; 
         break; 
       }
         i++; 
      } 
   printf(“\n\n no of char (incl.blanks) = %d”, chs); 
   printf(“\n No. of words = %d”, wds); 
   printf(“\n No of lines = %d”, ins); 
   getch() ; 
}
Output :

Enter the text, type $ at end
What is a string? How do you initialize it? Explain with example.
With example: $
No of char: (inch. Blanks) = 63
No of words = 12
No of lines =1.