Problem Description
Enter a string of characters to count the number of letters, number of digits, space words, and other characters (up to 100 characters) of the string character
Input
Multiple sets of test data, one set per line
Output
Each group output one line, the number of letters, number of digits, number of spaces and other characters
Sample Input
I am a student in class 1.I think I can!
Sample Output
18 1 6 110 0 3 1
HINT
Char str[100];//defines an array of character types
while (gets (str)!=null)//multiple sets of data
{
Input code
for (i=0;str[i]!= '; i++)//gets function automatically adds '% ' after STR as the end flag
{
Input code
}
The expression of a character constant,
' A ' denotes character A;
' 0 ' denotes character 0;
Assignment of characters
Str[i]= ' a ';//means assigning character A to Str[i]
str[i]= ' 0 ';//means assigning character 0 to Str[i]
}
My Code:
1#include <stdio.h>2 intMain ()3 { 4 Charch[ -];5 while(Gets (CH)!=null)6 7 {8 intchar_num=0, kongge_num=0, int_num=0, other_num=0, I;9 for(i=0; ch[i]!=' /'; i++)Ten One { A if(ch[i]>='A'&& ch[i]<='Z'|| ch[i]<='Z'&& ch[i]>='a') - { -char_num++; the } - Else if(ch[i]==' ') - { -kongge_num++; + } - Else if(ch[i]>='0'&&ch[i]<='9') + { Aint_num++; at } - Else - { -other_num++; - } - } inprintf"%d %d%d%d\n", char_num,int_num,kongge_num,other_num); - } to return 0; +}
Other code:
1#include <stdio.h>2#include <string.h>3 intMain ()4 {5 Charstr[ -];//defining an array of character types6 intA=0, b=0, c=0, d=0;//letters, numbers, spaces, and other7 while(Gets (str)! =NULL) {8 intLength =strlen (str);9 for(inti =0; i < length; i++){Ten if(Str[i] >='A'&&str[i] <='Z'|| Str[i] >='a'&&str[i] <='Z'){ Onea++; A } - Else if(Str[i] = =' '){ -C++; the } - Else if(Str[i] >='0'&&str[i] <='9'){ -b++; - } + Else{ -d++; + } A } atprintf"%d %d%d%d\n", A, B, C, d); -A=0; b=0; C=0; D=0; - } - return 0; -}
1#include <iostream>2#include <cctype>3 using namespacestd;4 5 Chars[ -];6 intMain ()7 {8 intAlpha,num,space,other;9 while(gets (s))Ten { OneAlpha=0, num=0, space=0, other=0; A for(intI=0; s[i]!=' /';++i) - { - if(Isalpha (s[i)) + +Alpha; the Else if(IsDigit (s[i)) + +num; - Else if(s[i]== +) ++space; - Else++Other ; - } +cout<<alpha<<" "<<num<<" "<<space<<" "<<other<<Endl; - } + return 0; A}
Wuhan University of Science and Technology acm:1003:0 starting point algorithm 67--statistics alphanumeric number