String Manipulation in C : A Comprehensive Guide


The string is a sequence of characters terminated with a null character \0. The string.h header defines one variable type, one macro, and various functions for manipulating arrays of characters. Strings are always enclosed by double quotes. Whereas, character is enclosed by single quotes in C.

Example :
     char x [ ] = " String " ;
     or
     char x [ 10 ] = { ' R ' , ' A ' , ' M ' , ' \0 ' } ;
     or
     char x [ 10 ] = " Tutor joes " ;

Difference between above declarations are, when we declare char as char x [ 10 ], 10 bytes of memory space is allocated for holding the string value. #inclued<String.h> header file supports all the string functions in C. All the string functions are given below.

  • strcmp ( str1 , str2 ) => String compares the str1 with str2. If both strings are same, it returns 0.
  • strcat ( str1 , str2 ) => String concatenates str1 with str2. Result of the string is stored in str1.
  • strcpy ( destination , source ) => String copies the contents of source string to destination string.
  • strrev ( str ) => String returns reverse string.
  • strlwr ( str ) => String lower returns string characters in lowercase.
  • strupr ( str ) => String upper returns string characters in uppercase.
  • strlen ( str ) => Find the length of a given string.

Source Code

#include<stdio.h>
#include<string.h>
 
int main()
{
    char c[20],a[20];
    char x[10]={'R','A','M','\0'};
    char y[10]={'K','U','M','A','R','\0'};
    printf("x : %s",x);
    printf("\nEnter The String : ");
    gets(c);
 
    printf("\nCompare   : %d ",strcmp(x,c));//String Compare
    printf("\nLength    : %d ",strlen(c));//String Length
    printf("\nReverse   : %s ",strrev(c));//String Reverse
    printf("\nUppercase : %s ",strupr(c));//String Upper
    printf("\nLowercase : %s ",strlwr(c));//String Lower
    printf("\nCopy      : %s ",strcpy(a,c));//String Copy
    strcat(x,y);
    printf("\nConcatenation : %s ",x);//String Concatenation
    return 0;
}
 
 
/*
char c[10];
printf("Enter The String : ");
//scanf("%s",c);
gets(c);
printf("%s",c);
*/
 
To download raw file Click Here

Output

x : RAM
Enter The String : sam

Compare   : -1
Length    : 3
Reverse   : mas
Uppercase : MAS
Lowercase : mas
Copy      : mas
Concatenation : RAMKUMAR

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C