3

I'm trying to create a recursive function which removes the consecutive duplicate characters from a string. It works fine except the first few characters. For example if my input is MMMMMuuuuuOOOOOKKKKLLLEE OOOOLLL or something like this, output is MMuOKLE OL. As you can see except for the first two M's it works fine. How can I make this work for the first part too? Here is my code:

#include <stdio.h>

char* remove_duplicates (char* str){
    if(*(str+1)!='\0'){
        if(*str==*(str+1)){
            *(str+1)=*(str+2);
             remove_duplicates(str+1);
        }
        remove_duplicates(str+1);
    }
    return str;
}

int main()
{
    char sample[] = "MMMMMuuuuuOOOOOKKKKLLLEE OOOOLLL";

    printf("OLD: |%s|\n", sample);
    printf("NEW: |%s|\n", remove_duplicates(sample));

    return 0;
}
4

5 回答 5

1

给你。

#include <stdio.h>

char * remove_duplicates( char *s )
{
    if ( *s )
    {
        if ( *s == *( s + 1 ) )
        {
            *( s + 1 ) = *( s + 2 );
            remove_duplicates( s + 1 );
            remove_duplicates( s );
        }
        else
        {
            remove_duplicates( s + 1 );
        }           
    }

    return s;
}

int main(void) 
{
    char s[] = "MMMMMuuuuuOOOOOKKKKLLLEE";

    remove_duplicates( s );

    puts( s );

    return 0;
}

程序输出为

MuOKLE
于 2020-04-28T22:29:46.480 回答
1

我是这样做的:

#include <stdio.h>

char* remove_duplicates(char* str)
{
    if (*str)
    {
        char* dest = remove_duplicates(str + 1);
        str = (*str == *dest) ? dest : ((*(dest - 1) = *str), (dest - 1));
    }
    return str;
}

int main()
{
    char sample[] = "MMMMMuuuuuOOOOOKKKKLLLEE OOOOLLL";
    char sample2[] = "AA";

    printf("OLD: |%s|\n", sample);
    printf("NEW: |%s|\n", remove_duplicates(sample));

    printf("OLD: |%s|\n", sample2);
    printf("NEW: |%s|\n", remove_duplicates(sample2));

    return 0;
}

输出

OLD: |MMMMMuuuuuOOOOOKKKKLLLEE OOOOLLL|
NEW: |MuOKLE OL|
OLD: |AA|
NEW: |A|
于 2020-04-28T21:52:56.887 回答
0

我在这里只是添加递归函数(语言:C++)而不是整个代码。

void removeConsecutiveDuplicates(char input[]) {
   
    if(input[0] == '\0' || input[1]=='\0') return;
    if(input[0]!=input[1]) return removeConsecutiveDuplicates(input+1);
    else
    {
        for(int i=1;i<=strlen(input);i++)
        {
            input[i-1] = input[i];
        }
       return removeConsecutiveDuplicates(input);
    }  
    return; 
}
于 2020-12-01T07:57:03.897 回答
0

谢谢大家的帮助。正如你所说,我在纸上浏览了我的代码,并意识到问题是它没有比较第一个和最后一个 M。我添加了一个新的 if 语句if(*str==*(str-1)) *(str)=*(str+1);,它现在可以工作了。

现在的功能是:

char* remove_duplicates (char* str){
    if(*(str+1)!='\0'){
        if(*str==*(str+1)){
            *(str+1)=*(str+2);
            remove_duplicates(str+1);
        }
        remove_duplicates(str+1);
    }
    if(*str==*(str-1)) *(str)=*(str+1);
    return str;
}
于 2020-04-28T21:50:10.733 回答
-1

我认为递归太复杂了。

让我们从 2 光标开始str

首先在字符串上循环。虽然我们没有到达*p字符串的末尾,但请期待p++

while (*p++) {
    if (*p == *current)
        continue;

如果下一个字符与当前相同,则继续搜索下一个不同的字符。

current++;
*current = *p;

当找到不同的字符时,只需将其放在当前字符之后。

#include <stdio.h>

char* remove_duplicates (char* str){
    char *p = str;
    char *current = p;
    while (*p++) {
        if (*p == *current)
            continue;
        current++;
        *current = *p;
    }

    return str;
}

int main()
{
    char sample[] = "MMMMMuuuuuOOOOOKKKKLLLEE OOOOLLL";

    printf("OLD: |%s|\n", sample);
    printf("NEW: |%s|\n", remove_duplicates(sample));
    printf("NEW: |%s|\n", remove_duplicates(""));

    return 0;
}


OLD: |MMMMMuuuuuOOOOOKKKKLLLEE OOOOLLL|
NEW: |MuOKLE OL|
NEW: ||

AAAB c 表示电流的详细信息

p
v
AAAB0
^
c

   p
   v
AAAB0
^
c

   p
   v
AAAB0
 ^
 c

   p
   v
ABAB0
 ^
 c

    p
    v
ABAB0
 ^
 c


    p
    v
ABAB0
  ^
  c


    p
    v
AB0B0
  ^
  c

我们得到AB

于 2020-04-28T21:35:23.927 回答