输入一串字符(包括字母和空格),统计并输出单词个数

本文介绍了一个简单的C语言程序,该程序用于计算输入文本中单词间的空格数量。程序通过getchar()函数逐字符读取输入,并利用计数器统计不包含连续空格的单词间隔。

 C语言作业

#include <stdio.h>
int main()
{
    int start = 0;//start用来说明输入有字母
    int i = 0;
    int j = 0;//i 为计数器,j为判断符
    char c = 0;

    while (c != '\n') {
   
        c = getchar();
        if (c == ' ') {
            ++j;//读入多个'空格'时,判断器不断增加,但只有判断器j=1时计数器才增加
        }
        else if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
            j = 0;//读入字母,重置判断器
            start = 1;//有字母
        };
        if (j == 1) {//判断器读入首个‘空格’,计数器+1
            ++i;
        }
  
    }

    if (j == 0&&start==1) {
        //结尾没有空格,j=0,i少了一个,所以i要+1
        printf("%d", i+1);
    }
    else if(j==1){
        //结尾有一个空格,但是最后回车是以'\n'形式进入循环,j此时为1,i会多一个
        printf("%d", i-1);
    }
    else {//结尾有好几个空格
        printf("%d", i );
    }
    return 0;
}

以下分别给出 Python C、C++ 语言实现从键盘输入字符统计数字、字母空格其他字符个数的代码: ### Python 实现 ```python word = 0 num = 0 other = 0 space = 0 a = input("请输入一串字符: ") for k in a: # 是否为单词 if k.isalpha(): word += 1 # 是否为数字 elif k.isdigit(): num += 1 elif k == ' ': space += 1 else: other += 1 print('字母个数是:', word) print('数字的个数是: ', num) print('空格个数是: ', space) print('其他字符个数是: ', other) ``` ### C 语言实现 ```c #include <stdio.h> #include <string.h> #include <ctype.h> //提供isalpha,isdigit,isspace等函数 #define MAX_LEN 1000 //最大输入长度 int main(void) { char str[MAX_LEN + 1]; //留一个位置给末尾的'\0' int letters = 0, digits = 0; int spaces = 0, others = 0; //提示读取一行输入(可包含空格) printf("请输入一个字符串(最多%d字符):\n", MAX_LEN); if (!fgets(str, sizeof(str), stdin)) { //fgets 读取整行,包括空格,安全避免缓冲区溢出 //如果读取失败,退出 fprintf(stderr, "读取输入失败。\n"); return 1; } //去掉末尾可能的换行符 str[strcspn(str, "\n")] = '\0'; //strcspn(str, "\n") 找到替换末尾换行符,确保后续遍历干净。 //遍历字符串每个字符,分类计数 for (size_t i = 0; str[i] != '\0'; i++) { unsigned char C = (unsigned char)str[i]; if (isalpha(C)) { //判断是否是字母 letters++; } else if (isdigit(C)) { //判断是否是数字 digits++; } else if (isspace(C)) { //判断是否是空格 spaces++; } else { others++; } } //输出结果 printf("\n统计结果:\n"); printf("字母:%d\n", letters); printf("数字:%d\n", digits); printf("空格:%d\n", spaces); printf("其他:%d", others); return 0; } ``` ### C++ 实现 ```cpp #include <iostream> #include <string> using namespace std; void statisNum(string& str) { int len = str.length(); int arr[4] = { 0 }; for (int i = 0; i < len; i++) { if (str[i] >= 'A' && str[i] <= 'Z') //比较 ASCII 码得值,判断区间归属 arr[0]++; else if (str[i] >= 'a' && str[i] <= 'z') arr[0]++; else if ('0' <= str[i] && str[i] <= '9') arr[1]++; else if (str[i] == ' ') arr[2]++; else if (str[i] != '\0') //如果前边的都没有且没有结束,就算其他的字符区间 arr[3]++; } cout << "字母个数为:" << arr[0] << endl; cout << "数字的个数为:" << arr[1] << endl; cout << "空格个数为:" << arr[2] << endl; cout << "其他的个数为:" << arr[3] << endl; } int main() { string S; char c; while ((c = cin.get()) != '\n') //若没有换行则一直输入字符 { S += c; //存入字符串 string } cout << S << endl; statisNum(S); } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值