Tuesday, February 26, 2019

program to perform various String Operations - in C Language using Ubuntu

File Name : string.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);
          //strcpy(pos1,pos2);
        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);
          //strcat(pos1,pos2);
        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 : guru
Enter Second String : nath
 Enter Your Choice :
1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.
Enter Your Choice :1
nath nath

Note : here strcpy function copies string in position2 copies to string in position1

Output 2:
Enter First String : guru
Enter Second String : nath
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 : guru
Enter Second String : guru
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 : guru
Enter Second String : nath
Enter Your Choice :
1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.
Enter Your Choice :3
gurunath nath

Enter First String : nath
Enter Second String : guru
Enter Your Choice :
1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.
Enter Your Choice :3
nathguru guru

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

Output 4:
Enter First String : guru
Enter Second String : nammu
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

Hadoop Commands

HADOOP COMMANDS OS : Ubuntu Environment Author : Bottu Gurunadha Rao Created: 31-Jan-2022 Updated: 31-Jan-2022 Release  : 1.0.1 Purpose: To ...

Search This Blog