Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c上获取循环中的字符串和字符输入_C - Fatal编程技术网

在c上获取循环中的字符串和字符输入

在c上获取循环中的字符串和字符输入,c,C,事实上,最近我发现了一个问题,我需要在给定的stringwith测试用例中计算一个给定的oj字符的出现次数。所以我写了这段代码,但输出并不是我想要的。我是一个初学者,所以我非常感谢任何有指导性的建议或帮助。谢谢 #include<stdio.h> #include<string.h> int main () { int ara [123]; char s[1000]; int l, j, i, len; char c; scan

事实上,最近我发现了一个问题,我需要在给定的stringwith测试用例中计算一个给定的oj字符的出现次数。所以我写了这段代码,但输出并不是我想要的。我是一个初学者,所以我非常感谢任何有指导性的建议或帮助。谢谢

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

int main ()
{
    int ara [123];
    char s[1000];
    int l, j, i, len;
    char c;

    scanf ("%d\n", &l);

    while (l >= 0){

        for (i = 65; i <= 122; i++)
        {
            ara[i] = 0;
        }

        fgets(s, 1000, stdin);
        len = strlen(s);

        for (i = 0;i <= len; i++)
        {
            ara[s[i]]++;
        }

        scanf(" %c\n", &c);
        j = c;
        printf("count : %d\n", ara[j]);

        l--;
    }

    return 0;
}
问题是scanf在输入中留下了一个新行,作为目标句子来阅读

您可以使用FGET和sscanf来解决此问题。我还添加了一些提示,以便更容易地了解预期结果

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

int main (void)
{
    int ara [128];                          // increased array size
    char s[1000];
    char t[10];
    int l, j, i, len;
    char c;

    printf("Enter how many loops:\n");
    fgets(t, sizeof t, stdin);              // replace scanf
    sscanf (t, "%d\n", &l);                 // with sscanf

    while (l > 0){                          // changed end test

        for (i = 'A'; i <= 'z'; i++)        // replaced magic numbers
        {
            ara[i] = 0;
        }

        printf("Enter the string:\n");
        fgets(s, sizeof s, stdin);
        len = strlen(s);

        for (i = 0;i <= len; i++)
        {
            ara[s[i]]++;
        }

        printf("Enter the letter:\n");
        fgets(t, sizeof t, stdin);          // replace scanf
        sscanf (t, "%c\n", &c);             // with sscanf
        j = c;
        printf("count : %d\n", ara[j]);

        l--;
    }

    return 0;
}
程序会话

Enter how many loops: 2 Enter the string: one two three four Enter the letter: o count : 3 Enter the string: three seven Enter the letter: e count : 4
请注意,理想情况下,还应该检查fgets和sscanf的函数返回值。

欢迎使用堆栈溢出。请花点时间阅读并参考您可以在此处询问的内容和方式。解决此类问题的正确工具是调试器。在询问堆栈溢出之前,应该逐行检查代码。如需更多帮助,请阅读。至少,您应该[编辑]您的问题,以包括一个重现您的问题的示例,以及您在调试器中所做的观察。如果发布的任何答案解决了您的问题,请记住接受它。这有助于我们大家知道,不需要采取进一步的行动。当这个答案发布时,我正在写我自己的答案。所以现在我看不出有任何理由发表我的观点,因为原则是一样的。但我认为有几件事需要补充:1初始化整个ara数组。目前该代码已被删除。在从stdin读取的程序中,初始化所有内容所需的额外时间并不重要。2在将JT用于索引到Ara之前,检查其值是否在范围内;3在索引到Ara之前,检查s[i]的值ara@4386427谢谢,我最后只提到了错误检查。该数组可以有256个元素,并且具有无符号字符输入。还有一些我本可以提到的事情,但我想让答案贴近问题,而不是彻底重写。同意——有时候最好专注于主要问题,忽略次要问题。我只是想给OP一个机会来获得一些额外的反馈+1从这里开始。