Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

Remove adjacent duplicates in a string in C

How to remove all adjacent duplicates in a string in C. say for example..if "caaabbcdd" is the given string then it should remove sequentially as

 1. cbbcdd

 2. ccdd

 3. dd

thus an empty string is returned in the end. Time complexity can be O(n^2) for starting.Can anyone help.

so far this i what i have done

void recursiven2(char *str)
{
int i,j,k,len;
    len=strlen(str);
    for(i=0;i<len-1;i++)
    {
    if(str[i]==str[i+1])
    {
        for(j=i;j<len-2;j++)
            str[j]=str[j+2];
        str[j]='';
    }
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can refer to this. It has a very nice explanation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...