69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
int main()
{
int word_num = 0;
char ch;
printf("请输入英文句子:");
while ((ch =getchar()) !='\n')
{
if (ch==' ')
word_num++;
else
word_num++;
}
printf("\n您输入的句子的字数为:%d\n",word_num);
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int word_num = 0;
char ch;
printf("请输入英文句子:");
while ((ch =getchar()) != EOF)
{
if (ch==' ' || ch == '\n')
word_num++;
}
printf("\n您输入的句子的字数为:%d\n",word_num);
system("pause");
return 0;
}
#include <stdio.h>
#include <stdbool.h>
int main (void) {
char text[81];
int countWords (char string[]);
printf ("Enter some stirng:\n");
gets(text);
printf ("Words=%i\n",text,countWords(text));
return 0;
}
int countWords (char string[]) {
int i,wordCount=0;
bool lookingForword=true;
bool alphabetic (const char c);
for(i=0;string[i]!='\0';i++) {
if(alphabetic(string[i])) {
if(lookingForword) {
wordCount++;
lookingForword=false;
}
}
else{
lookingForword=true;
}
}
return wordCount;
}
bool alphabetic (const char c) {
if((c>='a' && c<='z') || (c>='A' && c<='Z')) {
return true;
}else{
return false;
}
}