input string from user and print repeating or same in C



C Program Take input string from user and print repeating or same (characters either in sequence or non- sequence) sub string. (Don't Consider upper-lower case)

Sample Example:
input : my mom and my dad are good
output : my-my

input : Rose id not ores.
output : rose - ores

input : aaa AB BB a bA
output : aaa-a AB-bA


CODE :

#include <stdio.h>
#include <string.h>

void main()
{
    char str[50],wrd[15][25],flag[15][25];
    int asc[15];
    int i,j,k,n,sum=0;
    clrscr();

    i=j=k=n=0;

    /*input string from the user */
    printf("Enter your input string:");
    gets(str);

    /* copying each and every word from the string */
    while (str[i] != '\0')
    {
        if (str[i] == ' ')
        {
            wrd[j][k] = '\0';
            k = 0;
            j++;
        }
        else
        {
            wrd[j][k] = str[i];
            k=k+1;
        }
        i++;
    }

    wrd[j][k] = '\0';
    n = j;
   
    for(i=0;i<=n;i++)
    {
        strcpy(flag[i],wrd[i]);
        strlwr(wrd[i]);
    }

    for(i=0;i<=n;i++)
    {
        sum=0;
        for(j=0;wrd[i][j]!='\0';j++)
        {
            if(wrd[i][j]!=wrd[i][j+1])
            {
                sum=wrd[i][j];
             //        printf("%d",sum);
            }
        }
        asc[i]=sum;
    }

    /* Logic  */
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<=n;j++)
        {
            if (asc[i]==asc[j])
            {
                printf("%s",flag[i]);
                printf("%s ",flag[j]);
            }
        }
    }
    getch();
}
input string from user and print repeating or same in C input string from user and print repeating or same in C Reviewed by Unknown on 6:46 pm Rating: 5

No comments: