Saturday 14 July 2012

REVERSE a string and if any charecter is repeating then keep that one comes last





#include<stdio.h>
#include<string.h>
int main()
{
    char S[20],S1[20];
    int i,x,j,l,A[26]={0},count=0;
    gets(S);            // UPPER CASE ONLY
    l=strlen(S);
    for(i=0;i<l;i++)
    {
        x=S[i]-'A'+1;
        if(!A[x])
            count++;
        A[x]++;
    }
    S1[count]=NULL;
    for(j=l-1,i=0;count>i;)
    {
        x=S[j]-'A'+1;
        if(A[x]>0)
        {
            S1[i++]=S[j];
            A[x]=0;
        }
        j--;
    }
    printf("\n> %s",S1);
return 0;
}

No comments:

Post a Comment