在使用C字符串中删除重复相邻 [英] Remove adjacent duplicates in a string in C

查看:510
本文介绍了在使用C字符串中删除重复相邻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除所有相邻重复在C.字符串地说example..ifcaaabbcdd是给定的字符串那么就应该为

依次取出

  1 cbbcdd 2. CCDD 3. DD

这样一个空字符串,最终返回。时间复杂度可为O(n ^ 2)starting.Can任何人的帮助。

到目前为止我我做了什么

 无效recursiven2(字符*海峡)
{
INT I,J,K,LEN;
    LEN = strlen的(STR);
    对于(i = 0; I< LEN-1;我++)
    {
    如果(STR [I] ==海峡[I + 1])
    {
        为(J = I; J< LEN-2; J ++)
            海峡[J] = STR [J + 2];
        海峡[J] ='\\ 0';
    }
    }}


解决方案

您可以参考的。它有一个非常好的解释。

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]='\0';
    }
    }

}

解决方案

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

这篇关于在使用C字符串中删除重复相邻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆