Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.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,我试图写一个程序,输入一个句子,并计算各种各样的字符数。比如说, 输入: 输入以“.”或“!”结尾的句子或“?”:这*是一个 样本 句子 输出: 字符总数:28 元音数量:8 换行数:3 选项卡数量:1 空间数:2 其他字符数:14 到目前为止,我已经: #include<stdio.h> #include<ctype.h> int main() { char c; int i; printf("Enter a sentence (ended by a '.'

我试图写一个程序,输入一个句子,并计算各种各样的字符数。比如说,

输入:

输入以“.”或“!”结尾的句子或“?”:这*是一个
样本
句子

输出:

字符总数:28
元音数量:8
换行数:3
选项卡数量:1
空间数:2
其他字符数:14

到目前为止,我已经:

#include<stdio.h>
#include<ctype.h>

int main()
{
char c;
int i;
    printf("Enter a sentence (ended by a '.' or '!' or '?'):\n");
    scanf("%c", &c);

while (c !='.' && c !='?' && c !='!')

switch(c)
    {
        case 'a': i++;

        case 'e': i++;

        case 'i': i++;

        case 'o': i++;

        case 'u': i++;
    }
return 0;
   }
#包括
#包括
int main()
{
字符c;
int i;
printf(“输入一个句子(以“.”或“!”或“?”结尾):\n”);
scanf(“%c”、&c);
而(c!='。&&c!='?'&&c!='!')
开关(c)
{
案例“a”:i++;
案例“e”:i++;
案例“i”:i++;
案例“o”:i++;
案例“u”:i++;
}
返回0;
}
简而言之,我需要编写一个程序,一个字符一个字符地读取一个句子,并计算字符总数 元音、换行符、制表符、空格和所有其他字符的数量。用户输入的句子将结束 带句号、问号或感叹号。程序不会计算句号、感叹号、, 或者问号作为一个字符。 有人能帮我吗?
//更新:我需要使用while循环和switch编写它。我相信事情会层叠发生,但这是必要的。

你可以这样做

#include <stdio.h>

int main()
{
    char c;
    int end = 0;
    int vowels = 0;
    int newlines = 1;
    int tabs = 0;
    int spaces = 0;
    int others = 0;
    int total = 0;

    printf("Enter a sentence (ended by a '.' or '!' or '?'):\n");

    while(!end) {
        scanf("%c", &c);
        switch(c) {
            case '.':
            case '?':
            case '!':
                end = 1;
                break;
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
                vowels++;
                break;
            case '\n':
                newlines++;
                break;
            case '\t':
                tabs++;
                break;
            case ' ':
                spaces++;
                break;
            default:
                switch(c) {
                    case 'a' ... 'z':
                    case 'A' ... 'Z':
                        others++;
                        break;
                }
                break;
        }
    }

    total = vowels + newlines + tabs + spaces + others;
    printf("Total number of characters: %d\n", total);
    printf("Number of vowels: %d\n", vowels);
    printf("Number of newlines: %d\n", newlines);
    printf("Number of tabs: %d\n", tabs);
    printf("Number of spaces: %d\n", spaces);
    printf("Number of other characters: %d\n", others);

    return 0;
}
#包括
int main()
{
字符c;
int end=0;
int元音=0;
int换行符=1;
int tabs=0;
int空间=0;
int其他=0;
int-total=0;
printf(“输入一个句子(以“.”或“!”或“?”结尾):\n”);
当(!结束){
scanf(“%c”、&c);
开关(c){
案例“”:
案例“?”:
案例“!”:
结束=1;
打破
案例“a”:
案例“e”:
案例“i”:
案例“o”:
案例“u”:
案例“A”:
案例“E”:
案例“I”:
案例“O”:
案例“U”:
元音++;
打破
案例“\n”:
换行符++;
打破
案例'\t':
tabs++;
打破
案例“”:
空格++;
打破
违约:
开关(c){
案例“a”…“z”:
案例“A”…“Z”:
其他++;
打破
}
打破
}
}
总计=元音+换行符+制表符+空格+其他;
printf(“字符总数:%d\n”,总计);
printf(“元音数:%d\n”,元音);
printf(“换行数:%d\n”,换行);
printf(“选项卡数量:%d\n”,选项卡);
printf(“空格数:%d\n”,空格);
printf(“其他字符数:%d\n”,其他);
返回0;
}

检查有关开关的C手册。每一个案例都只是“跌破”到下一个案例,没有
break
语句。因此,循环将每个
a
5次,每个
e
4次,等等。此外,您只读取一个字符,然后进入循环,不再读取任何字符。您可能应该将整行内容读入一个
char
数组,然后遍历循环中的
char
数组,每次一个
char
。或者,您可以使用
getchar()
一次读取输入的一个字符,并在看到行尾或句尾时停止。如果您有一个查找表,您可以执行
i+=value[c]
其中
value
是一个256个条目的数组,对于元音,它大部分是
0
1
。。。。或256个字符类型的条目数组和5个字符计数的条目数组。然后你可以做
count[kind[c]]++
而不需要任何
switch
ing。除了@潜伏者的评论,你可以添加一个
default:
case来计算“其他字符”。为了回应上面每个人的帖子,我需要使用while循环和switch编写它。我相信事情会层出不穷,但这是必要的。我应该在我最初的帖子中发布,但我现在正在更新它。