Code Competitions

Coding competition

Tuesday, 14 May 2013

Program to perform all string operations.

/* Program using string function */

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
    char str1[30],str2[30],str3[30];
    int choice,len;
    clrscr();

    printf("\n\n\t\t STRING FUNCTION\n\n");
   
    // Menu
    printf("\n\t\t Press 1 for String Copy.");
    printf("\n\t\t Press 2 for String Concatenation.");
    printf("\n\t\t Press 3 for String Length.");
    printf("\n\t\t Press 4 for String Reverse.");
    printf("\n\t\t Press 5 for String Compare.");
    printf("\n\t\t Press 6 for Lower case change.");
    printf("\n\t\t Press 7 for Upper case change.");
    printf("\n\t\t Press 8 for String search.");
    printf("\n\t\t Press any other key to exit.");
   
    // Entering choice
    printf("\n\n\t Enter your choice: ");
    scanf("%d",&choice);
    printf("\n\n\n\n");

    // Actual functioning using switch   
    switch(choice)
    {
        // String copy function
        case 1:
        {
            printf("\n Enter the first string: ");
            scanf("%s",str1);
            strcpy(str2,str1);
            printf("\n Second string: %s",str2);
            break;
        }
   
        // String concatination.
        case 2:
        {
            printf("\n Enter the first string: ");
            scanf("%s",str1);
            printf("\n Enter the second string: ");
            scanf("%s",str2);
            strcat(str1,str2);
            printf("\n\n Concatinated string: %s",str1);
            break;
        }
   
        // String length
        case 3:
        {
            printf("\n Enter the string: ");
            scanf("%s",str1);
            len=strlen(str1);
            printf("\n\n Length of string: %d",len);
            break;
        }

        // String reverse
        case 4:
        {
            printf("\n Enter the string: ");
            scanf("%s",str1);
            strrev(str1);
            printf("Reversed string: %s",str1);
            break;
        }

        // String compare
        case 5:
        {
            printf("\n Enter the first string: ");
            scanf("%s",str1);
            printf("\n Enter the second string: ");
            scanf("%s",str2);
            len=stricmp(str1,str2);
            if(len==0)
            {
                printf("\n\n Strings are equal.");
            }
            else
            {
                printf("\n\n Strings are different.");
            }
            break;
        }

        // Upper case change
        case 6:
        {
            printf("\n Enter the string: ");
            scanf("%s",str1);
            strupr(str1);
            printf("\n\n Changed case: %s",str1);
            break;
        }

        // Lower case change
        case 7:
        {
            printf("\n Enter the string in upper case: ");
            scanf("%s",str1);
            strlwr(str1);
            printf("\n\n Changed case: %s",str1);
            break;
        }

        // String search
        case 8:
        {
            printf("\n Enter the string: ");
            scanf("%s",str1);
            printf("\n Enter the string to be searched: ");
            scanf("%s",str2);
            strstr(str1,str2);
            printf("\n\n String after search: %s",str1);
            break;
        }

        default:
        {
            printf("\n\n Bye..!! Have good day..!!");
            exit(0);
        }
    }
    getch();
}

No comments:

Post a Comment