首页 > 试题广场 >

使用字符函数统计字符串中各类型字符的个数

[编程题]使用字符函数统计字符串中各类型字符的个数
  • 热度指数:6348 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
键盘录入一句话,统计这话中字母字符、数字字符、空白、标点符号和其它字符的个数。(使用字符函数实现)

输入描述:
键盘输入一个字符串


输出描述:
输出字符串中字母字符、数字字符、空白、标点符号和其它字符的个数。
示例1

输入

hello123world,$ 123

输出

chars : 10 whitespace : 1 digits : 6 others : 2
#include <iostream>
#include <string>
using namespace std;

void pmc(int* a,int* b,int* c,int* d,string s){
    for (int i = 0;i < s.length();i++)
    {
        if (isalpha(s[i]))
            (*a)++;
        else if (isdigit(s[i]))
            (*c)++;
        else if (isspace(s[i]))
            (*b)++;
        else
            (*d)++;
    }
}

int main() {
    string s;
    getline(cin,s);
    int a = 0,b = 0,c = 0,d = 0;
    pmc(&a,&b,&c,&d,s);
    cout << "chars : " << a
        << " whitespace : " << b
        << " digits : " << c
        << " others : " << d;
    return 0;
}
发表于 2022-05-18 22:42:33 回复(1)
    // write your code here......
    for(int i=0;i<str.length();i++)
    {
        if(str[i]>='A'&&str[i]<='z')
        {
        chars++;
        }
        else if(str[i]==' ')
        {
        whitespace++;
        }
        else if(str[i]>='0'&&str[i]<='9')
        {
        digits++;
        }
        else
        {
        others++;
        }
    }
专业爱护小白20周年:)
发表于 2021-11-29 17:09:28 回复(1)
#include <iostream>
#include <string>

using namespace std;

int main() {
    string str;
    getline(cin, str);

    int whitespace = 0;
    int digits = 0;
    int chars = 0;
    int others = 0;

    // write your code here......
    for(int i = 0; i < str.size(); ++i){
        if(str[i]>='a' && str[i]<='z')      chars++;
        else if(str[i]>='A' && str[i]<='Z') chars++;
        else if(str[i]>='0' && str[i]<='9') digits++;
        else if(str[i]==' ')                whitespace++;
        else                                others++;
    }
    //end

    cout << "chars : " << chars
        << " whitespace : " << whitespace
        << " digits : " << digits
        << " others : " << others << endl;

    return 0;
}

编辑于 2024-02-04 09:55:42 回复(0)
#include <iostream>
#include <string>
using namespace std;

int main(int argc, const char *argv[])
{
    string s1;
    getline(cin, s1);

    int *save = new int[5]{0};
    for (auto &x : s1)
    {
        isalpha(x) ? save[0]++:  isspace(x)   ? save[1]++
                              :  isdigit(x)   ? save[2]++
                              :  ispunct(x)   ? save[3]++
                              :  save[4]++;
    }
    cout << "chars : " << save[0] << " "
         << "whitespace : " << save[1] << " "
         << "digits : " << save[2] << " "
         << "others : " << save[3] + save[4] << " "
         << endl;

    return 0;
}

发表于 2022-11-05 10:22:13 回复(0)
#include <bits/stdc++.h>
#include <iostream>
#include <string>

using namespace std;

int main() {

    string str;
    getline(cin, str);

    int whitespace = 0;
    int digits = 0;
    int chars = 0;
    int others = 0;

    // write your code here......
    for (int i = 0; i < str.length(); i++)
    {
        if (str[i] >= 'A' && str[i] <= 'z') {
            chars++;
        }
        else if (str[i] == ' ') {
            whitespace++;
        }
        else if (str[i] >= '0' && str[i] <= '9') {
            digits++;
        }
        else {
            others++;
        }
    }

    cout << "chars : " << chars
        << " whitespace : " << whitespace
        << " digits : " << digits
        << " others : " << others << endl;

    return 0;
}

发表于 2022-01-15 08:43:11 回复(4)
#include <iostream>
#include <string>

using namespace std;

int main() {

    string str;
    getline(cin, str);

    int whitespace = 0;
    int digits = 0;
    int chars = 0;
    int others = 0;

    for (char i : str) {
        if (i == ' ') {
            whitespace++;
            continue;
        }
        if (i >= '0' && i <= '9') {
            digits++;
            continue;
        }
        if (i >= 'a' && i <= 'z' || i >= 'A' && i <= 'Z') {
            chars++;
            continue;
        }
        others++;
    }


    cout << "chars : " << chars
         << " whitespace : " << whitespace
         << " digits : " << digits
         << " others : " << others << endl;

    return 0;
}

发表于 2023-04-25 00:28:38 回复(0)
#include<iostream>
using namespace std;
#include <string>
int main()
{
	string str;
	getline(cin, str);
	int chNum = 0, spNum = 0, dgNum = 0, otherNum = 0;
	for (int i = 0; i < str.size(); i++)
	{
		if (isalpha(str[i]))
			chNum++;
		else if (isalnum(str[i]))
			dgNum++;
		else if (str[i] == ' ')
			spNum++;
		else
			otherNum++;
	}
	printf("chars : %d whitespace : %d digits : %d others : %d", chNum, spNum, dgNum, otherNum);
	return 0;
}

发表于 2023-02-08 18:30:29 回复(0)
#include <iostream>
#include <cstring>
using namespace std;

int main(){
    string s;
    getline(cin,s);

    int chars = 0;
    int whitespace = 0;
    int digits = 0;
    int others = 0;

    for(int i = 0;i < s.length();i++){
        if(isalpha(s[i])){
            chars++;
        }
        else if(isspace(s[i])){
            whitespace++;
        }
        else if(isdigit(s[i])){
            digits++;
        }
        else{
            others++;
        }
    }

    cout << "chars : " << chars << " whitespace : " << whitespace 
        << " digits : " << digits << " others : " << others << endl;

    return 0;
}
// 64 位输出请用 printf("%lld")
发表于 2022-10-24 11:17:12 回复(0)
#include <iostream>
#include <cstdio>

using namespace std;

int main(){
    string str;
    getline(cin,str);

    int chars=0,ws=0,digits=0,others=0;
    for(int i=0;i<str.length();i++){
        char t=str[i];
        if(isalpha(t)) chars++;
        else if(isdigit(t)) digits++;
        else if(t==' ') ws++;
        else others++;
    }
    printf("chars : %d whitespace : %d digits : %d others : %d",chars,ws,digits,others);

    return 0;
}

发表于 2022-10-06 20:52:42 回复(0)
#include <stdio.h>      // scanf()
#include <stdbool.h>    // bool

#define MAX_SIZE 100    // 字符数组大小

// 判断:是否是英文字符
bool is_chars(char ch) {
    return ('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z');
}

// 判断:是否是数字字符
bool is_digit(char ch) {
    return '0' <= ch && ch <= '9';
}

// 判断:是否是空白字符
bool is_space(char ch) {
    return ch == ' ' || ch == '\t' || ch == '\f' || ch == '\n' || ch == 'r' || ch == '\v';
}

// 实际功能函数
void func(const char * str, int * chars, int * whitespaces, int * digits, int * others) {
    // 1. 初始化传入传出变量
    *chars = 0; *whitespaces = 0; *digits = 0; *others = 0;

    // 2. 完成功能逻辑,使用for循环,遍历每位字符,并暴力判断
    for (const char * p = str; *p != '\0'; ++p) {
        if (is_chars(*p)) {
            ++*chars;
        } else if (is_digit(*p)) {
            ++*digits;
        } else if (is_space(*p)) {
            ++*whitespaces;
        } else {
            ++*others;
        }
    }
}

int main() {
    char    str[MAX_SIZE] = {0};
    scanf("%[^\n]", str);   // 获取一行字符串,并存储到str字符数组中
    int     chars = 0, whitespaces = 0, digits = 0, others = 0;

    func(str, &chars, &whitespaces, &digits, &others);
    printf("chars : %d whitespace : %d digits : %d others : %d", chars, whitespaces, digits, others);
}
发表于 2022-09-30 16:46:44 回复(0)
题目要求使用字符串函数,故考虑到isalpha、isalnum等函数;另外,用指针可以快速解决这种遍历问题,所以先把string转成char数组,用一个指针就能很快得出结果;
#include <iostream>
#include <cstring>
using namespace std;

int cnt_chars, cnt_blank, cnt_digits, cnt_others;

int main() {
    string src;
    getline(cin, src);

    char dst[src.size()];
    strcpy(dst, src.c_str());

    char* p = dst;
    while (*p != '\0') {
        isalpha(*p)? cnt_chars++ : (isalnum(*p)? cnt_digits++ :(*p==' '? cnt_blank++ : cnt_others++));
        p++;
    }

    cout <<"chars : "<<cnt_chars<< " whitespace : "<<cnt_blank<< " digits : "<<cnt_digits<<" others : "<<cnt_others; 
}


发表于 2022-09-17 22:37:53 回复(0)
#include <iostream>
#include <string>

using namespace std;

int main() {

    string str;
    getline(cin, str);

    int whitespace = 0;
    int digits = 0;
    int chars = 0;
    int others = 0;

    // write your code here......
    for (int i = 0; i < str.length(); ++i) {
        if (str[i] == ' ') {
            ++whitespace;
        } else if (str[i] >= '0' && str[i] <= '9') {
            ++digits;
        } else if (str[i] >= 'A' && str[i] <= 'Z' || str[i] >= 'a' && str[i] <= 'z') {
            ++chars;
        } else {
            ++others;
        }
    }

    cout << "chars : " << chars
        << " whitespace : " << whitespace
        << " digits : " << digits
        << " others : " << others << endl;

    return 0;
}

发表于 2022-09-02 14:16:07 回复(0)
#include <iostream>
#include <string>

using namespace std;

int main() {

    string str;
    getline(cin, str);

    int whitespace = 0;
    int digits = 0;
    int chars = 0;
    int others = 0;
    
    for(auto x:str)
    {
        if(isalpha(x)) chars++;
        else if(isdigit(x)) digits++;
        else if(isspace(x)) whitespace++;
        else others++;     
    }

    // write your code here......
    

    cout << "chars : " << chars
        << " whitespace : " << whitespace
        << " digits : " << digits
        << " others : " << others << endl;

    return 0;
}
发表于 2022-08-15 09:40:20 回复(0)
发表于 2022-07-18 21:09:17 回复(0)
#include <iostream>
#include <string>
 
using namespace std;
 
int main() {
 
    string str;
    getline(cin, str);
 
    int whitespace = 0;
    int digits = 0;
    int chars = 0;
    int others = 0;
 
    // write your code here......
    for (int i = 0; i < str.length(); i++)
    {
        if (str[i] >= 'A' && str[i] <= 'z') {
            chars++;
        }
        else if (str[i] == ' ') {
            whitespace++;
        }
        else if (str[i] >= '0' && str[i] <= '9') {
            digits++;
        }
        else {
            others++;
        }
    }
 
    cout << "chars : " << chars
        << " whitespace : " << whitespace
        << " digits : " << digits
        << " others : " << others << endl;
 
    return 0;
}

发表于 2022-07-16 09:36:18 回复(0)
#include <iostream>
#include <string>

using namespace std;

int main() {

    string str;
    getline(cin, str);

    int whitespace = 0;
    int digits = 0;
    int chars = 0;
    int others = 0;

    // write your code here......
    int i = 0;
    while(str[i] != '\0'){
        if(str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z')
            chars++;
        else if(str[i] >= '0' && str[i] <= '9')
            digits++;
        else if(str[i] == ' ')
            whitespace++;
        else
            others++;
        i++;
    } 

    cout << "chars : " << chars
        << " whitespace : " << whitespace
        << " digits : " << digits
        << " others : " << others << endl;

    return 0;
}

发表于 2022-07-10 14:12:01 回复(0)
#include <iostream>
#include <string>

using namespace std;
#include <string>

int main() {

    string str;
    getline(cin, str);

    int whitespace = 0;
    int digits = 0;
    int chars = 0;
    int others = 0;

    // write your code here......
    int i;
    while (str[i] != '\0') {
        if (isspace(str[i])) {
            whitespace++;
            } else if (isdigit(str[i])){
            digits++;
            }else if (isalnum(str[i]) ){
                chars++;
            } else{
                others++;
            }
            i++;
}

cout << "chars : " << chars
     << " whitespace : " << whitespace
     << " digits : " << digits
     << " others : " << others << endl;

return 0;
}
发表于 2022-03-26 16:39:44 回复(0)
发表于 2021-12-18 21:19:25 回复(0)
#include <iostream>
#include <string>

using namespace std;

int main() {

    string str;
    getline(cin, str);

    int whitespace = 0;
    int digits = 0;
    int chars = 0;
    int others = 0;

    // write your code here......
    char p;
    for(int i = 0;str[i]!='\0';i++){
        p = str[i];
        if(p>='A'&&p<='Z'||p>='a'&&p<='z') chars++;
        else if(p==' ') whitespace++;
        else if(p>='0'&&p<='9') digits++;
        else others++;
    }

    cout << "chars : " << chars
        << " whitespace : " << whitespace
        << " digits : " << digits
        << " others : " << others << endl;

    return 0;
}
发表于 2021-11-28 20:52:13 回复(0)

问题信息

难度:
19条回答 748浏览

热门推荐

通过挑战的用户

查看代码