C++统计字符串中各类型字符的个数

#include <iostream>
#include <cstring>
using namespace std;

int main() {

    int letter = 0;
    int digit = 0;
    int space = 0;
    int other = 0;
    
    char buf[1024] = {0};
    cin.getline(buf, sizeof(buf));

    // write your code here......
    for(int i = 0; buf[i] != '\0'; i++){ //遍历字符串到'\0'为止
        char c = buf[i];
        if(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') //大小写字母
            letter++;
        else if(c >= '0' && c <= '9') //数字
            digit++;
        else if(c == ' ')  //空格
            space++; 
        else //其他字符
            other++;
    }
    cout << "letter:" << letter << " digit:" << digit << " space:" << space << " other:" << other << endl;

	return 0;
}

#include <iostream>
#include <cstring>
using namespace std;

int main() {

    int letter = 0;
    int digit = 0;
    int space = 0;
    int other = 0;
    
    char buf[1024] = {0};
    cin.getline(buf, sizeof(buf));

    // write your code here......
    int len = strlen(buf);
    for(int i = 0;i < len; i++){
        if(isalpha(buf[i])){
            letter++;
        }else if(isdigit(buf[i])){
            digit++;
        }else if(isspace(buf[i])){
            space++;
        }else{
            other++;
        }
    }
    cout << "letter:" << letter << " digit:" << digit << " space:" << space << " other:" << other << endl;

	return 0;
}

  • 4
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用字符串的遍历来统计0的个数。具体实现步骤如下: 1. 定义一个计数器变量count,初始值为0。 2. 使用for循环遍历字符串的每一个字符。 3. 如果当前字符是'0',则将count加1。 4. 循环结束后,count变量的值即为字符串0的个数。 下面是示例代码: ```c++ #include <iostream> #include <string> using namespace std; int main() { string str = "102040506070"; int count = 0; for(int i = 0; i < str.length(); i++) { if(str[i] == '0') { count++; } } cout << "0的个数为:" << count << endl; return 0; } ``` 输出结果为: ```c++ 0的个数为:2 ``` ### 回答2: 要统计字符串0的个数,可以使用循环遍历字符串的每个字符,判断是否为0,并计数。具体过程如下: 1. 初始化一个计数器变量count为0,用于记录0的个数。 2. 使用循环遍历字符串的每个字符。 3. 判断当前字符是否为0,如果是,则计数器count加1。 4. 循环结束后,count的值就是字符串0的个数。 下面是一个示例代码: ```python def count_zeros(string): count = 0 # 初始化计数器为0 for char in string: # 遍历字符串的每个字符 if char == '0': # 判断当前字符是否为0 count += 1 # 是0则计数器加1 return count # 测试示例 string = "12043005" zero_count = count_zeros(string) print("字符串0的个数为:", zero_count) ``` 以上代码,定义了一个名为`count_zeros`的函数,接受一个字符串作为参数,返回字符串0的个数。通过调用这个函数,传入一个待统计字符串,即可得到字符串0的个数。在示例统计字符串为`"12043005"`,最终输出的结果为3,即字符串0的个数为3。 注意,以上代码是使用Python语言编写的,如果在其他语言环境下,需要将代码适当修改。 ### 回答3: 要统计一个字符串0的个数,可以使用循环遍历字符串的每个字符,判断是否为0。计数变量初始为0,每当遇到一个0时,计数变量就加1。最后输出计数变量的值就是字符串0的个数。 以下是一个简单的示例代码: ```python def count_zeros(s): count = 0 for ch in s: if ch == '0': count += 1 return count s = input("请输入一个字符串:") zero_count = count_zeros(s) print("字符串0的个数为:", zero_count) ``` 该代码首先定义了一个名为`count_zeros`的函数,接收一个字符串作为参数。函数的`count`变量用于记录0的个数,初始值为0。接下来使用`for`循环遍历字符串的每个字符,判断是否为0,如果是则将`count`加1。最后,函数返回计数的值。 在主程序,我们使用`input`函数获取用户输入的字符串,并调用`count_zeros`函数统计0的个数。最后,将统计结果输出给用户。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值