输入一行字符,统计单词的个数,单词之间一空格隔开。
#include <stdio.h>
#include <string.h>
void * s_get(char *st,int n);
int main(void) {
char ch[100];
int i,s,count;
i = count = 0;
s_get(ch,100);
s = strlen(ch);
while(i < s){
while(ch[i] == ' '){/*跳过空格*/
i++;
}
if(i < s){
count++;
}
while(ch[i] != ' '&& i < s){/*跳过一个单词*/
i++;
}
}
printf("%d",count);
return 0;
}
void * s_get(char *st,int n){
char * rev;
char * find;
rev = fgets(st,n,stdin);
if(rev){
find = strchr(st,'\n');
if(find){
* find = '\0';
}
else{
while(getchar() != '\n'){
continue;
}
}
}
return rev;
}
输入:
Practice makes perfect
输出:
3