从键盘输入的字符串由英文的句子组成(if you are doing your best, you will not have to worry about.)。(注意:单词之间可能不止一个空格)
完成:对输入字符串进行英文字母个数,空格个数,英文单词个数的统计,并且显示统计结果。
用devc++平台编写
#include<stdio.h>
#include<string.h>
int main()
{
void number(char *p);
char str[100];
gets(str);
number(str);
return 0;
}
void number(char* p)
{
int letter=0,word=1,space=0;
while (*p != '\0') //字符串的结束符号位"\0"
{
if (('A' <= *p) && (*p <= 'z'))
letter++;
else if (*p == ' ')
{
space++;
word++;
}
p++;
}
printf("字母有:%d个﹐空格有:%d,单词有:%d",letter,space,word);
}
#include<ctype.h>
#include<string.h>
#include<stdio.h>
int main()
{
char buf[1024];
gets(buf);
int len = strlen(buf);
int i = 0, space_num = 0, char_num = 0, word_num = 0;
while (i < len)
{
if (!isspace(buf[i]) && !isalpha(buf[i]))
{
++i;
continue;
}
while (i < len && isspace(buf[i]))
{
++space_num;
++i;
}
if (i < len && isalpha(buf[i]))
{
++word_num;
while (i < len && isalpha(buf[i]))
{
++char_num;
++i;
}
}
}
printf("字母个数:%d,空格个数:%d,英文单词个数:%d\n",
char_num, space_num, word_num);
}