|
发表于 2020-2-18 15:06:49
From FishC Mobile
|
显示全部楼层
|阅读模式
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<string.h>
int main()
{
char string[100];
int i,num=0,word=0,letter=0;
char c;
gets(string);
for(i=0;(c=string[i])!='\0';i++)
{if(c>='a'&&c<='z'||c>='A'&&c<='Z')
{letter++;
}
}
for(i=0;(c=string[i])!='\0';i++)
{if(c==' ') word=0;
else if(word==0)
{word=1;
num++;
}
}
printf("There are %d words and %d letters",num,letter);
return 0;
}
代码没有问题,规范的代码应该是这样的:
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char string[100];
- int i, num = 0, word = 0, letter = 0;
- char c;
- gets(string);
- for (i = 0; (c = string[i]) != '\0'; i++)
- {
- if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
- {
- letter++;
- }
- }
- for (i = 0; (c = string[i]) != '\0'; i++)
- {
- if (c == ' ')
- word = 0;
- else if (word == 0)
- {
- word = 1;
- num++;
- }
- }
- printf("There are %d words and %d letters", num, letter);
- return 0;
- }
复制代码
|
|