【问题标题】:How to reset this string scanning code for a new letter?如何重置此字符串扫描代码以获取新字母?
【发布时间】:2021-08-25 13:25:58
【问题描述】:

我正在做作业,快完成了。这是我的代码

#include <stdio.h>
#include <stdlib.h>

void main()
{
    char word[100],*ptr,input;
    int count,i,n;
    printf("Enter the word : ");
    scanf("%[^\n]", word);
    ptr = &word;
    do
    {
        printf("Enter the letter : ");
        scanf(" %c", &input);
        if(input == '-'){
                printf("Exit.");
                return 0;
        }
        for(i=0;word[i] != '\0';i++)
        {
            if(*ptr == input)
                count++;
            ptr++;
        }
        printf("Has %d of '%c'",count,input);
        printf("\n");
    }while(input != '-')
}

此代码将提取字符串中的字母数

例如:如果您输入“WWWWooo”,它将给出“有 4 个 'W'”和“有 3 个 'o'”

当您输入“-”字母时,代码将退出。

我发现的问题是“计数”值似乎仍然是我要查找的第一个字母的总数。

我的期望:“WWWWooo”将给出“有 4 个‘W’”和“有 3 个‘o’”

我得到的是“有 4 个 'W'”和“有 4 个 'o'”。计数值未重置。

我尝试将 count = 0; 放在 do while 循环的顶部,扫描的第二个字母的输出将始终为 0。

我该如何解决这个问题?

【问题讨论】:

  • 把 count = 0 放在最后,在 printf 之后
  • 不仅count需要重置,ptr也需要重置。但无论如何你都可以摆脱ptr。将*ptr == input 替换为word[i] == input。这更有意义,因为您在 for 条件和正文中使用相同的变量
  • 请仔细检查您是否显示了minimal reproducible example。显示的代码 a) 无法编译 b) 有明显的修复,不符合描述的行为。
  • @Fabiobreo 我试过了,每次都返回 0
  • 您需要同时进行评论提案,Fabios 和 Ingos。 (并修复一些其他问题......)然后我得到Enter the word : Enter the letter : Has 4 of 'W' Enter the letter : Has 3 of 'w' Enter the letter : Exit. 例如这里tutorialspoint.com/compile_c_online.php请学习阅读和修复编译器警告,不仅仅是错误。

标签: c loops


【解决方案1】:

我稍微更改了您的代码,我想这就是您要查找的内容(代码中的 cmets 几乎解释了我所做的所有更改):

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char word[100],*ptr,input;
    int count,i,n;
    printf("Enter the word : ");
    scanf("%99[^\n]", word); //changed to %99[^\n] to avoid buffer overflows
    do
    {
        count=0; //count=0 should be here as count++ changes the value of count afterwards
        ptr=word; //ptr=word should be here too since ptr++ changes the value of ptr afterwards
        /* you might have probably gotten a compiler warning because
        of ptr=&word, since &word is char(*)[100] (a pointer to an array of 100 chars)
        and char(*)[100] and char * are quite different,
        so you can either do ptr=word or ptr=&word[0] (both of which point to the first character in the array) */
        //the rest of the code below is basically the same
        printf("Enter the letter : ");
        scanf(" %c", &input);
        if(input == '-'){
                printf("Exit.");
                return 0;
        }
        for(i=0;word[i] != '\0';i++)
        {
            if(*ptr == input)
                count++;
            ptr++;
        }
        printf("Has %d of '%c'",count,input);
        printf("\n");
    }while(input != '-'); //you had missed a ';' here
}

尽管我没有在上面的代码中检查scanf() 的返回值,但是正如@Yunnosch 在评论中提到的那样,这样做总是一个好主意。

【讨论】:

  • 修复了!非常感谢!
  • 我喜欢int main 和微妙不同的ptr=word;。我建议也用 cmets 覆盖它们。我很乐意看到 scanf 返回值的检查,或者在那里轻推评论。但你已经得到了我的支持。
  • @Yunnosch 谢谢!我添加了一条关于为什么在代码中使用ptr=word 的评论。
猜你喜欢
  • 1970-01-01
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多