23.12.19

String Operations program in c

#include <stdio.h>


#include <string.h>


void main()


{


    char str1[30],str2[30];


    int choice,result=0;


    int len1=0,len2=0;


    printf("\n Enter First String : ");


    scanf("%[^\n]",str1);


    printf("\n Enter Second String : ");


    scanf("%s",str2);


    printf("\n Enter Your Choice : \n");


    printf("1. Copy Strings. \n");


    printf("2. Compare Strings. \n");


    printf("3. Concatenate Strings. \n");


    printf("4. Find Lengths. \n");


    printf("\n Enter Your Choice :");


    scanf("%d",&choice);


    switch(choice)


    {


       case 1 :


         {
        strcpy(str1,str2);


        printf("\n %s %s \n",str1,str2);

        break;
         }


       case 2 :


         {


        result = strcmp(str2,str1);


        if(result == 0)


           printf("\n Both Strings are Same. ");


        else


           printf("\n Both are different strings.");




        break;


         }


       case 3 :


         {


        strcat(str1,str2);



        printf("\n %s %s \n",str1,str2);


        break;


         }


       case 4 :
         {


        len1 = strlen(str1);


        printf("\n Legth of First String is %d \n",len1);


        len2 = strlen(str2);


        printf("\n Legth of Second String is %d \n",len2);


        break;


         }


     default : printf("\n Wrong Choice .");


    }


   


}




Output1:
Enter First String : sana


Enter Second String : siri


 Enter Your Choice :

1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.


Enter Your Choice :1
siri siri



Output 2:



Enter First String : sana


Enter Second String : siri


Enter Your Choice :


1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.
 Enter Your Choice :2


 Both are different strings.

Enter First String : sana
Enter Second String : sana


Enter Your Choice :
1. Copy Strings.

2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.


Enter Your Choice :2


Both Strings are Same.

Output 3:


Enter First String : siri

Enter Second String : sana


Enter Your Choice :


1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.


Enter Your Choice :3
sanasiri siri



 Note : strcat() function string in position1 will be concatinated with string in position2

Output 4:


Enter First String : sana


Enter Second String : deepu


Enter Your Choice :
1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.


Enter Your Choice :4


Legth of First String is 4
Legth of Second String is 5

No comments:

Post a Comment

JAVA program pattern 2

import java.util.Scanner; public class pattern3 { public static void main(String args[]) { int i,j; Scanner sc=new Scanner(System.in); Syste...