利用字符数组编写程序来统计字符串“look,if you had one shot,one opportunity,to seize everything you ever wanted."的单词总个数和单词的平均字符数
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "look,if you had one shot,one opportunity,to seize everything you ever wanted.";
int len = strlen(str);
int word_count = 0;
int char_count = 0;
int i;
for (i = 0; i < len; i++) {
if (str[i] == ' ' || str[i] == ',' || str[i] == '.') {
if (char_count > 0) {
word_count++;
char_count = 0;
}
} else {
char_count++;
}
}
if (char_count > 0) {
word_count++;
}
printf("单词总个数:%d\n", word_count);
printf("单词的平均字符数:%.2f\n", (float) len / word_count);
return 0;
}