Pages

Friday

Program to Perform Various String Operations on a Given String in C

Here is a C program to perform different manipulations on a given string. This program encapsulates different operations provided by C language on a String e.g.
  • finding length of a string
  • converting a string to uppercase or lowercase
  • reversing a string
  • concatenating of two strings
  • copy one string to another string
This program uses C concepts like  Array, Gets Function and String Functions like string length (strlen()), string copy (strcpy()),strcat(), strrev(), strlwr() and strupr().

# include <stdio.h>
# include <conio.h>
# include <string.h>

void main()
{
char str1[40], str2[40] ;
clrscr() ;
printf("Enter the first string : \n\n") ;
gets(str1) ;
printf("\nEnter the second string : \n\n") ;
gets(str2) ;
printf("\nString 1 = %s & String 2 = %s ", str1, str2) ;
printf("- Length is : %d and %d", strlen(str1), strlen(str2)) ;
printf("\n\nString 1 = %s & String 2 = %s ", str1, str2) ;
printf("- Uppercase is : %s and %s", strupr(str1), strupr(str2));
printf("\n\nString 1 = %s & String 2 = %s ", str1, str2) ;
printf("- Lowercase is : %s and %s", strlwr(str1), strlwr(str2));
printf("\n\nString 1 = %s & String 2 = %s ", str1, str2) ;
printf("- Reverse is : %s and %s", strrev(str1), strrev(str2)) ;
printf("\n\nString 1 = %s & String 2 = %s ", str1, str2) ;
printf("- String copy is : %s ", strcpy(str1,str2));
printf("\n\nString 1 = %s & String 2 = %s ", str1, str2) ;
printf("- Concatenation is : %s ", strcat(str1,str2));
printf("\n\nString 1 = %s & String 2 = %s ", str1, str2) ;
getch() ;
}


Output of above program

Enter the first string :
Football

Enter the second string :
Rocks!

String 1 = Football & String 2 = Rocks! - Length is : 8 and 6
String 1 = Football & String 2 = Rocks! - Uppercase is : FOOTBALL and ROCKS!
String 1 = FOOTBALL & String 2 = ROCKS! - Lowercase is : football and rocks!
String 1 = football & String 2 = rocks! - Reverse is : llabtoof and !skcor
String 1 = llabtoof & String 2 = !skcor - String copy is : !skcor
String 1= !skcor & String 2= !skcor - Concatenation is : !skcor!skcor
String 1 = !skcor!skcor & String 2 = !skcor

No comments:

Post a Comment