#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; }
#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; }
#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; }
#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; }
#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; }
#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; }
#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); }
#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; }
#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; }
#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; }