|
| » 首页 » 电脑_数码 » 编程 » 紧急!!!编写程序:输入一行字符,统计其有多少个单词,单词... |
紧急!!!编写程序:输入一行字符,统计其有多少个单词,单词... |
|
|
![]() |
|
|
main() {char c; int word=0,speac=0; printf("请输入一行字符:\n"); while((c=getchar())!='\n') {if(c>='a'&&c<='z'||c>='A'&&c<='Z') word=1+speac; else if(c==' ') speac++; } printf("单词=%d,空格数=%d\n",word,speac); } main() {char c; int word=0,speac=0; printf("请输入一行字符:\n"); while((c=getchar())!='\n') {if(c>='a'&&c<='z'||c>='A'&&c<='Z') word=1+speac; else if(c==' ') speac++; } printf("单词=%d,空格数=%d\n",word,speac); } 楼主是来转移积分的吧。我看你连什么是单词都没搞清楚。 #include <stdio.h> #include <ctype.h> int countWords(void) { char c; int i, state; // state of 1 in a word, 0 out of a word; i as counter // get input from stdin, press Ctrl+D to send EOF in *nix, Ctrl+Z in M$ Windows for (i = 0, state = 0; (c=getchar()) != EOF; state = isspace(c) ? 0 : (!state ? (++i,1) : state)); // increament i at the start of a word return i; } int main(void) { printf("%d\n", countWords()); return 0; } |
| 《紧急!!!编写程序:输入一行字符,统计其有多少个单词,单词...》答案收集时间:2008-06-14 14:47:14 |