String operations using Built in functions

Program Description

Implements basic string operations like calulating length,concatenation,copy etc using standard library string functions.The objective of this code is just to demonstrate the use of the standard library functions. No error checking has been performed while taking user input

Program
/*******************************************
*    code.cheraus.com
*
*    String operations using Built in functions
*  
*******************************************/

#include<stdio.h>
#include<string.h>
int main()

{
 //string length arbitrarily taken as 20. Can take as per requirement
 char str1[20],str2[20];
 int i,opt,m,n;

do
 {
  printf("Enter the first string=");
  scanf("%s",str1);
 
  printf("Enter the second string=");
  scanf("%s",str2);
 
 
   //length calulation of given string using strlen() function
   m  =  strlen(str1);
   n  =  strlen(str2);
 

 //Creating menu for the user
 printf("\n\nEnter the option\n1.To Find the Length Of Strings\n2.Concatenate the strings\n3.Copy the strings\n4.Exit\nYour choice: ");
 scanf("%d",&i);

 switch(i)
 {
   case 1:
         printf("\nThe length of first string is '%d'\n The lenght of second sring is '%d'\n",m,n);
         break;
   case 2:
          //concatenation using strcat() function
          strcat(str1,str2);
           printf("\nThe new string is='%s'\n",str1);
           break;
       
  case 3: //copy using strcpy() function
          strcpy(str2,str1);
          printf("\nThe copied string in 2 is '%s'\n",str2);
          break;

  case 4 : return 0;

  default :printf("\nInvalid choice........Try again\n");
           break;
          }

        printf("\nDo you wish to continue.\nPress 1 to continue...Any key to exit\n");
        scanf("%d",&opt);
 } while(opt==1);
  return 0;
  }

Output

Enter the first string=cheraus
Enter the second string=code
Enter the option
1.To Find the Length Of Strings
2.Concatenate the strings
3.Copy the strings
4.Exit
Your choice: 1
The length of first string is '7'
The lenght of second sring is '4'
Do you wish to continue.
Press 1 to continue...Any key to exit
1
Enter the first string=che
Enter the second string=raus
Enter the option
1.To Find the Length Of Strings
2.Concatenate the strings
3.Copy the strings
4.Exit
Your choice: 2
The new string is='cheraus'
Do you wish to continue.
Press 1 to continue...Any key to exit
1
Enter the first string=cheraus
Enter the second string=code
Enter the option
1.To Find the Length Of Strings
2.Concatenate the strings
3.Copy the strings
4.Exit
Your choice: 3
The copied string in 2 is 'cheraus'
Do you wish to continue.
Press 1 to continue...Any key to exit
2

Comments
comments powered by Disqus