如何编辑我的代码以便输出用户输入的两个特定字符的频率? (C语言)

How do I edit my code so that I may output the frequency of TWO specific characters input by a user? (C Language)

我已经尝试了几个小时来编辑此代码以使其按我希望的方式工作。我打算编写一个程序来跟踪下划线“_”和感叹号“!”出现的频率。出现在用户输入的句子中: _Hi_there!!!

必须使用的规范and/or未删除 - 这个函数原型必须保持不变: -必须使用getchar()方法来保存用户输入的句子的值

经过数小时的努力获得正确的输出后,现在我有了这段代码:

#include <stdio.h>

int num_count;
int num_exclamation_count;
char ch;
char s[1000]; 
void count(int* num_, int* num_exclamation)
{       
    int i;
    for(i = 0; s[i] != '[=10=]'; i++)
    {
        if((s[i] == getchar()) && (s[i] == '_'))
        {
            ++num_count;
            *num_ = num_count;  
        }
        else if((s[i] == getchar()) && (s[i] == '!'))
        {
            ++num_exclamation_count;
            *num_exclamation = num_exclamation_count;
        }
        else
        {
            continue;
        }
    }

}

int main (void)
{
    int num_user, num_exclamation_user;
    printf("Enter a sentence: ");
    do
    {
        ch = getchar();
    }while(ch != '\n');
    count(&num_user, &num_exclamation_user);

    printf("There are %d underscores and %d exclamation points in the 
    sentence.\n", num_user, num_exclamation_user);

    return 0;
}

我得到的输出如下:

There are 0 underscores and 0 exclamation points in the sentence.

我尝试了 while 或 if 或 do-while 语句的所有变体,我可以在脑海中想到或在网上找到可用的语句,但我无处可去,但离得更远了。如果有人能彻底解释我如何使用 getchar() 方法和必要的变量来安排条件 statement/loop,那就太棒了。如果我公然搞砸了某些事情或忽略了一个明显的问题,也要接受批评,不要害怕伤害我的感情。我只想学习,这将是我唯一的心态,因为我得到了帮助解决这个问题。

谢谢。

您的程序正在获取 "sentence" 作为用户输入然后完全忽略它。

你没有初始化这个变量。

int num_user;
int num_exclamation_user;

我建议您将字符串输入存储在某处(例如 s),如下所示:

fgets (s, 1000, stdin);

而不是你的 while do 循环。 然后在您的 count 函数中,只需将 s[i]'_''!' 进行比较 像这样:

if(s[i] == '_'){

        ++num_count;
        *num_ = num_count;  
    }
else if (s[i] == '!')
    {
        ++num_exclamation_count;
        *num_exclamation = num_exclamation_count; 
    }
    else
    {
        continue;
    }

因为 s[i] == getchar() 你要求用户 re-type 一个字符。

字符串 s 如何获得对用户输入内容的引用?

您可能希望将 ch 插入到主循环中的字符串中。

int count = 0;
do
{
    ch = getchar();
    s[count] = ch;
    count = count + 1;
}while(ch != '\n');

当然,用户不应键入超过 1000 个字符,否则将失败。但是,您可以在 while 循环中添加一个条件来检查 count 来解决这个问题。不过总的来说,您应该考虑使用 secure 输入法,这可能不在您的问题和限制范围内,但了解一下还是很好的。

为什么要将 getchar() 放在 count() 函数中?您打算从标准输入(控制台)接受更多字符吗?如果没有,您只需从该函数中删除 getchar() 部分

void count(int* num_, int* num_exclamation)
{       
    int i;
    for(i = 0; s[i] != '[=11=]'; i++)
    {
        if(s[i] == '_')
        {
            ++num_count;
            *num_ = num_count;  
        }
        else if(s[i] == '!')
        {
            ++num_exclamation_count;
            *num_exclamation = num_exclamation_count;
        }
        // This is not really required
        //else
        //{
        //    continue;
        //}
    }

}

另外,在main()函数中初始化num_usernum_exclamation_user

getchar() 是一个从输入中提取下一个字符并 return 的函数。 次调用它,都会检索下一个字母。

在您的代码中,main() 中的循环获取了整个输入并且...没有将其放在任何地方,因此它永远丢失了。

您似乎应该将其保存到 s[]。然后,在count()中,你可以将当前字符引用为s[I],而不是调用getchar(),这对return没有更多的输入。

OP 的代码有这些错误:

  1. 未在 main() 中初始化 num_user, num_exclamation_user。将它们设置为 0

  2. main()do { ch = getchar(); while (ch != '\n'); 不需要的代码。删除

  3. 从具有 for(i = 0; s[i] != '[=16=]'; i++) 的输入中寻找 空字符 阻止了循环迭代,因为 s[0] 被初始化为 01。用户输入是 ,而不是 字符串 。寻找 '\n'。还赋值s[i] = getchar(),不比较s[i] == getchar()

    //for (i = 0; s[i] != '[=10=]'; i++) {
    for (i = 0; i < 1000 && (s[i] = getchar()) != '\n'; i++) {
    
  4. 代码每个循环多次调用 getchar()

    // if ((s[i] == getchar()) && (s[i] == '_')) {
    if (s[i] == '_') {
      ...
    // } else if ((s[i] == getchar()) && (s[i] == '!')) {
    } else if (s[i] == '!') {
    

尽管还有其他学习器问题(例如,使用 int ch 作为输入比 char s[i] 更好),解决上述问题将解决 OP 的问题。

1 全局整数类型像数组s[1000]元素初始化为0.

count() 下面。在鼠标悬停之前想清楚。

void count(int* num_, int* num_exclamation) {
内部通道; while ((ch = getchar()) != '\n' && ch != EOF) {
如果 (ch == '') {
(*num
)++;
} else if (ch == '!') {
(*num_)++;
}
}
}

getchar() return 类型是 int,因此将 ch 类型更改为 int 并添加对 EOF 的检查。此外,您不希望 \n 字符成为存储在 s 数组中的输入的一部分,因此,将 do while 循环替换为 while 循环。
您不需要全局变量 - num_countnum_exclamation_countch。尽量不要使用全局变量。即使您不需要将缓冲区 s 声明为全局变量。您可以在 main() 中声明它并将其作为参数传递给函数 count() 但是为此,您需要修改 count() 原型,这似乎是您不想要的去做。
其他答案已经指出了您代码中的错误。你可以这样做:

#include <stdio.h>

#define BUF_SZ 1000

char s[BUF_SZ];

void count(int* num_, int* num_exclamation)
{
    int i;
    for(i = 0; s[i] != '[=10=]'; i++)
    {
        if(s[i] == '_')
        {
            ++(*num_);
        }
        else if(s[i] == '!')
        {
            ++(*num_exclamation);
        }
    }
}

int main (void)
{
    int num_user = 0, num_exclamation_user = 0, i = 0, ch;

    printf("Enter a sentence: ");

    while ((ch = getchar()) != '\n' && ch != EOF)
    {
        if (i < (sizeof(s) - 1)) { // sizeof(s) - 1, to keep the space for terminating null character
            s[i++] = ch;
        } else {
            break; //discard the input beyond the size of buffer s
        }
    }

    s[i] = '[=10=]';
    count(&num_user, &num_exclamation_user);

    printf("There are %d underscores and %d exclamation points in the sentence.\n", num_user, num_exclamation_user);

    return 0;
}