top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Reduce the string by deleting two same adjacent letter in the string?

+2 votes
239 views

Reduce the string by deleting two same adjacent letter in the string for example "aabcccd" becomes bcd and "aaabccddd" becomes abd and "baab" becomes Empty (baab->bb->empty), hence print empty string.

posted Apr 20, 2016 by Aditya Dwivedi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes
 
Best answer

concept is to save those alphabets in the string S again which are adjacent but not same:example aabccd reduce to bd (b is saved at starting index of string S again and d is saved at the next index so here we simply skiped a and c ). same as with abba.It is reduced like: abba->aa->empty :

#include <stdio.h>
#include <string.h>
int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    char S[101];
    int lenStr; i,j=0,count;
    scanf("%s",S);
    char *p=S;
    recheck:
    count=0;
    j=0;
    lenStr=strlen(S);
    S[lenStr]=='1';
    for(i=0;i<lenStr;i++){
        if(p[i]==p[i+1]) {
          i++;
          count++;
        }
        else
            S[j++]=p[i];
    }
    S[j]='\0';
    if(count!=0)
        goto recheck;
    else
    {
        if(p[0]=='\0')
            printf("Empty String\n");
        else
            printf("%s",S);
    }
    return 0;
}
answer Apr 20, 2016 by Shahsikant Dwivedi
...