统计单词数 - 云代码 http://yuncode.net/code/c_50b56eff6668752
- /* 统计单词数 */
- #include <stdio.h>
- #include <string.h>
- #define MAX_STRING 200 /* 数组容量 */
- int main(void) {
- char str[MAX_STRING] = { 0 }; /* 定义str,并初始化为全0 */
- int i = 0;
- int length = 0;
- int count = 0;
- /* 输入字符数组 */
- printf("Input original String:");
- gets(str); /* 为str赋值 */
- length = strlen(str); /* 获得输入str的长度 */
- /* 统计单词个数 */
- for (i = 0; i < length; ++i) {
- if (str[i] != ' ') { /* 单词开始 */
- ++count;
- while (' ' != str[i] && '\0' != str[i]) /* 单词结束 */
- ++i;
- }
- }
- printf("There are %d words in \"%s\".\n", /* 输出统计结果 */
- count, str);
- return 0;
- }