Program to perform String length, reverse, comapre, concatinate & Upper case and lower case using user defined functions along with pointers.

#include<stdio.h>
#include<string.h>
main()
{
    int z;
    printf("Choose the operation.\n1. String Length\n2. String Reverse\n3. String Compare\n4. String Concatinate\n5. String Lower\n6. String Upper Case\n");
    scanf("%d",&z);
    switch(z)
    {

    case 1:
        {
            int i;
    char *p,str[500];
    printf("Enter a string\n");
    p=str;
    scanf("%s",str);
    for(i=0;*p!='\0';i++)
    {
        p++;
    }
    printf("Length of string is %d",i);
break;
        }

    case 2:
        {
            int i,j,k,l;
    char *p,*q,str[50];
    printf("Enter a string\n");
    p=str;
    scanf("%s",str);

    j=strlen(str);
    printf("Reverse of the string is ");
    for(i=j;i>=0;i--)
    {
        printf("%c",*(p+i));
    }
    break;
        }

    case 3:
        {
            char str1[50],str2[50],*a,*b;
            int i,j,k,l,m=0;
            printf("Enter 1st string\n");
            scanf("%s",str1);
            printf("Enter 2nd string\n");
            scanf("%s",str2);
            a=&str1;
            b=&str2;
            k=strlen(str1);
            l=strlen(str2);
            for(i=0;i<k;i++)
            {
                if(*(a+i)==*(b+i))
                {
                    m++;
                }
                else
                    {
                    printf("Not matched.");

                }

            }
            if(m==k)
            {
                printf("Matched");
            }
            break;

        }

    case 4:
        {
            int i,j=0,k,l,m,n,o;
    char *p,*q,str1[50],str2[50];
    printf("Enter string 1\n");
    scanf("%s",str1);
    printf("Enter string 2\n");
    scanf("%s",str2);
    p=str1;
    q=str2;
    k=strlen(str1);
    l=strlen(str2);
    m=k+l;
    n=k;
    for(i=0;i<=l;i++)
    {
        *(p+n)=*(q+i);
        n++;
    }
    printf("Concatination of 2 strings is ");
    for(i=0;i<=k+l-1;i++)
    {
        printf("%c",*p);
        p++;
    }
    break;
}
    case 5:
        {
            char str[50],*p;
            int i,j;
            printf("Enter an upper case string\n");
            scanf("%s",str);
            p=&str;
            j=strlen(str);
            for(i=0;i<j;i++)
            {
                *(p+i) = *(p+i) + 32;
                printf("%c",*(p+i));
            }
            break;

        }

    case 6:
        {
            char str[50],*p;
            int i,j;
            printf("Enter a lower case string\n");
            scanf("%s",str);
            p=&str;
            j=strlen(str);
            for(i=0;i<j;i++)
            {
                *(p+i) = *(p+i) - 32;
                printf("%c",*(p+i));
            }
            break;

        }

    default :
        {
            printf("This is not a valid option.\n");
        }

        }

     }

 

 

OUTPUT: [Click to enlarge]:

string operations

Leave a Reply

Your email address will not be published.