【问题标题】:Repeatedly removing and replacing the occurence of a substring from the input string从输入字符串中反复删除和替换出现的子字符串
【发布时间】:2015-04-20 15:30:50
【问题描述】:

我有这个作业问题:编写一个 C 程序,在使用函数反复将每次出现的 'foo' 替换为 'oof' 后,从输入字符串中删除子字符串 foo 的出现后找到新​​字符串。

这是我的代码:

#include <stdio.h>
#include <string.h>

void manipulate(char * a)
{
    int i, flag = 0;
    char newstr[100];

    for (i = 0; a[i] != '\0'; i++) {
        if (a[i] == 'f') {
            if ((a[i + 1] != '\0') && (a[i + 1] == 'o')) {
                if ((a[i + 2] != '\0') && (a[i + 2] == 'o')) {
                    i += 3;
                    flag++;
                }
            }
        }
        newstr[i] = a[i];
    }

    for (i = 0; i < flag; i++) {
        strcat(newstr, "oof");
    }

    printf("\nThe output string is %s", newstr);
}

int main()
{
    char a[100];

    printf("Enter the input string");
    scanf("%s",a);
    manipulate(a);

    return 0;
}

我认为我的代码有问题,因为预期的输出是:

Enter the input string
akhfoooo
The output string is akhoooof

但我的实际输出是:

Enter the input string
akhfoooo
The output string is akhoof

您能纠正我的代码中的错误吗?

【问题讨论】:

  • 这个scanf("%c",a);不应该是scanf("%s",a);
  • 谢谢,但即使更改后,我仍然没有得到预期的输出。
  • @geek_scuba_diver,karma_geek 是对的。对于您的编辑,请参阅我的回答。

标签: c string c-strings


【解决方案1】:

改变这个

scanf("%c",a);

到这里

scanf("%s",a);

因为你想读取一个字符串,而不是一个字符。


编辑您的编辑(见 cmets):

#include <stdio.h>
#include <string.h>

void manipulate(char * a)
{
    int i = 0;
    char *pch;
    size_t len = strlen(a);
    while(a[i]) // until we do not reach the null terminator
    {
        // so that we do not go after the end of the string
        if(i + 2 == len)
          break;
        // if we found 'foo'
        if(a[i] == 'f' && a[i + 1] == 'o' && a[i + 2])
        {
           // replace it with 'oof'
           a[i] = 'o';a[i + 1] = 'o'; a[i + 2] = 'f';
           i = i + 2; // and check for after the replacement we just made
        }
        // increment our counter to check the next charachter
        i = i + 1;
    }
    printf("\nThe output string is %s\n", a);
}

int main()
{
    char a[100];

    printf("Enter the input string");
    scanf("%s",a);
    manipulate(a);

    return 0;
}

如果你想使用一个函数,strstr() 会很好。

【讨论】:

  • 我实现了你的代码。我得到的输出是“akhoofoo”。我认为必须在末尾添加“oof”。
  • @geek_scuba_diver 你的意思是你运行我的代码。 :) 我确实按照您的输入运行并得到“akhoooof”,我认为这是正确的。
  • 是的,我运行了你的代码。这是我得到的: sh-4.3#gcc-omain*.c sh-4.3#main 输入输入字符串akhfoooo 输出字符串是akhoofoo
【解决方案2】:

这会很好。 注意边界条件和何时退出。

#include <stdio.h>
#include <string.h>

void manipulate(char * a)
{
    int i = 0;
    char *pch;
    size_t len = strlen(a);
    while(a[i]) // until we do not reach the null terminator
    {
    // if we found 'foo'
    if(a[i] == 'f' && a[i + 1] == 'o' && a[i + 2]=='o')
    {
    // replace it with 'oof'
    a[i] = 'o';a[i + 1] = 'o'; a[i + 2] = 'f';
    if ( i+3 == len){
    break;
    }
    else{

    i = i + 2; // and check for after the replacement we just made
    continue;
    }
    }
    else
{
    i = i+1;
}
    }
    printf("\n\n\nThe output string is %s \n", a);
    }

int main()
     {
     char a[100];

      printf("Enter the input string \n");
      scanf("%s",a);
      manipulate(a);

       return 0;
      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 2021-01-23
    • 2014-08-07
    • 1970-01-01
    • 2021-03-20
    • 2023-03-31
    相关资源
    最近更新 更多