Ex.No:5 solving problems using String functions

AIM:

         To write a C program to illustrate any five string functions.

ALGORITHM:

Step 1: Start the program.

Step 2: Read the string str1 and choice.

Step 3: If choice is 1

Print length of str1 using strlen()

      If choice is 2

Print reverse of  str1 using strrev()

      If choice is 3

Get another string str2

Compare str1 and str2 using strcmp()

      If choice is 4

Copy the first half of str1 to another str2 using strcpy()

Print str2

      If choice is 5

Get another string str2

Concatenate str1 and str2 using strcat()

Step 4: Stop the program.

PROGRAM CODE:

#include<stdio.h>
#include<conio.h>
void main()
{
    char str1[10],str2[10],original[10];
    int choice,half;
    clrscr();
    printf("n  Enter the string:");
    scanf("%s",&str1);
    strcpy(original,str1);
    printf("n  MENU");
    printf("n  1. String Length");
    printf("n  2. String Reverse");
    printf("n  3. String Compare");
    printf("n  4. String Copy");
    printf("n  5. String Concate");
    printf("n  6. Exit");
    while(1)
    {
        strcpy(str1,original);
        printf("n  Enter your choice:");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
            printf("  Length of %s is %d ",str1,strlen(str1));
            break;
            case 2:
            printf("  Reverse of %s is %s",original,strrev(str1));
            break;
            case 3:
            printf("  Enter another string for comparison:");
            scanf("%s",&str2);
            if(strcmp(str1,str2)==0)
            printf("  Strings are equal");
            else
            printf("  Strings are not same");
            break;
            case 4:
            half=strlen(str1)/2;
            strncpy(str2,str1,half);
            str2[half]='';
            printf("  The first half of %s is %s",str1,str2);
            break;
            case 5:
            printf("  Enter the string:");
            scanf("%s",str2);
            strcat(str1,str2);
            printf("  After Concatenation:%s",str1);
            break;
            default:
            exit(0);
        }
    }
}
OUTPUT:

string

RESULT:

                    Thus the C program to illustrate any five string functions is written and executed successfully.

Leave a Reply

Your email address will not be published. Required fields are marked *