在主函数中输入字符串及输出上述结果。
提示有错误,麻烦大仙改之。
int alph,digit,space,others;
main()
{
char text[80];
printf("\n输入字符串:\n");
gets(text);
printf("字符串是:");
puts(text);
strcat(text,"\n");
alph=0;
digit=0;
space=0;
others=0;
count(text);
printf("\n%d字母,%d数字,%d空格,%d其它字符\n",alph,digit,space,others);
}
count(str);
char str[];
{
int i;
for(i=0;str[i]!='\n';i++)
{
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
alph++;
else if(str[i]>='0'&&str[i]<='9')
digit++;
else if(str[i]==' ')
space++;
else
others++;
}
}
已经修改过了,主要是scanf输入时,它是以空格为结束标志,即它不能输入空格。要改用gets,它是以回车符为结束标志的。
#include <stdio.h>
#include <string.h>
void main()
{
void shuchu(char x[100]);
char a[100];
gets(a); /*改了此处*/
shuchu(a);
}
void shuchu(char x[100])
{
int letter, number, space, other;
int n, ch, i;
n = strlen(x);
letter = number = space = other = 0;
for (i = 0; i <= n - 1; i++)
{
ch = x[i];
if ('A' <= ch && ch <= 'Z' || 'a' <= ch && ch <= 'z')
letter++;
else if (ch == ' ')
space++;
else if ('0' <= ch && ch <= '9')
number++;
else
other++;
}
printf("letter=%d\n", letter);
printf("space=%d\n", space);
printf("number=%d\n", number);
printf("other=%d\n", other);
}