nerak


私信TA

用户名:nerak

访问量:1174

签 名:

等  级
排  名 28868
经  验 479
参赛次数 0
文章发表 3
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

解题思路:

解一:多组输入

解二:使用gets()

注意事项:

参考代码:

#include<stdio.h>
#include<string.h>
int main()
{
    int letters = 0, digits = 0, spaces = 0, others = 0;
    char ch;
    // 多组输入
    while(scanf("%c", &ch) != EOF)  //EOF表示end of file,其值为-1,输入时另起一行输入ctrl+z表示结束输入 // 还可以写成while(~scanf(""))形式  
    {
        if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
            letters ++;
        else if(ch >= '0' && ch <= '9')
            digits ++;
        else if(ch == ' ')
            spaces ++;
        else
            others ++;
    }

    printf("%d %d %d %d\n", letters, digits, spaces, others - 1);  // 最后一个回车键被记录到others中,因此要减去
    return 0;


}
#include<stdio.h>
#include<string.h>
int main()
{
    int letters = 0, digits = 0, spaces = 0, others = 0;
    char ch[10000];
    gets(ch);  /// 输入一行
    for(int i = 0; i < strlen(ch); i++)  /// strlen(ch):得到数组ch的长度
    {
        if((ch[i] >= 'a' && ch[i] <= 'z') || (ch[i] >= 'A' && ch[i] <= 'Z'))
            letters ++;
        else if(ch[i] >= '0' && ch[i] <= '9')
            digits ++;
        else if(ch[i] == ' ')
            spaces ++;
        else
            others ++;
    }

    printf("%d %d %d %d\n", letters, digits, spaces, others);
    return 0;


}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区