题目描述:
Word Counting
Problem Description
Each year on May the graduate students are busy writing thesis, and the graduation thesis needs thousands of words. It's not a simple matter. Counting the words correctly become an interesting job.
We all know that the thesis may include English words, numbers, punctuation marks, images, formulas, and so on.
During the word count, we assume that English words, a string of meaningful numbers and punctuation mark is valid statistics word.
The single character in English words or numbers can not be counted as a word.
For example, the word "acm" count as one word instead of three words, number "2011" counted as a word. Of course, such space and carriage returns(Enter) can't be considered a word.
Write a program to help graduates to test whether the number of words under the thesis requirement.
Input
There are multiple test case in input, each test case end with a single line "###", the input may contain english character, numbers, punctuation e.g. ':' , ',' , '+' , '-', and space, Enter.
Output
Output the number of word for each test case, and a separate line for each case.
Sample Input
A simple test ### Hunan University 2011 the 7th Programming Contest. ### The 5th Central South China Programming Contest. ###
Sample Output
3 8 8
Source
这个题目要注意的是,单个的标点符号算作一个单词。
A simple+-.ui test. 答案是:8 (simple+-.ui:simple + - . ui 共5个)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <ctype.h> 6 using namespace std; 7 8 const int maxn = 1000 + 10; 9 char s[maxn]; 10 11 int main() 12 { 13 int i, len, cnt = 0; 14 while (scanf("%s", s) != EOF) 15 { 16 if (strcmp(s, "###") == 0) 17 { 18 printf("%d ", cnt); 19 cnt = 0; 20 } 21 else{ 22 len = strlen(s); 23 bool mark = false; 24 for (i = 0; i < len; i++) 25 { 26 if (ispunct(s[i])) 27 { 28 cnt++; 29 mark = false; 30 } 31 else if (!mark) 32 { 33 cnt++; 34 mark = true; 35 } 36 } 37 } 38 } 39 return 0; 40 }