69,373
社区成员
发帖
与我相关
我的任务
分享
fgets(text,sizeof(text),stdin);
gets_s(text,sizeof(text))
[/quote]fgets(text,sizeof(text),stdin);
gets_s(text,sizeof(text))
[/quote]for (i = 0; i != '\0'; i++)
for (i = 0; str[i] != '\0'; i++)
fgets(text,sizeof(text),stdin);
gets_s(text,sizeof(text))
for (i = 0; i != '\0'; i++)
以上代码等同于for (i = 0; i !=0; i++)
所以。。。scanf("%s", text);
的话遇到空格就停止接收后面的字符,所以估计也达不到你的预期目的,应该如同sghcpt所述进行修改,改成gets(text);
或者scanf("%[^\n]", text);
#include<stdio.h>
int alph, num, spa, other;
int main()
{
void statistics(char str[]);
char text[80];
alph = 0;
num = 0;
spa = 0;
other = 0;
//scanf("%[^\n]", text);
gets(text);
statistics(text);
printf("alphabet=%d\nfigure=%d\nspace=%d\nother=%d\n", alph, num, spa, other);
return 0;
}
void statistics(char str[])
{
int i;
for (i = 0; str[i] != '\0'; i++)
if ((str[i] >= 'A'&&str[i] <= 'Z') ||( str[i] >= 'a'&&str[i] <= 'z'))
alph++;
else if (str[i] >= '0'&&str[i] <= '9')
num++;
else if (str[i] == ' ')
spa++;
else other++;
}
#include<stdio.h>
int alph, num, spa, other;
int main()
{
void statistics(char str[]);
char text[80] = { 0 };
alph = 0;
num = 0;
spa = 0;
other = 0;
scanf_s("%79[^\n]", text, (unsigned)_countof(text));
statistics(text);
printf("alphabet=%d\nfigure=%d\nspace=%d\nother=%d\n", alph, num, spa, other);
return 0;
}
void statistics(char str[])
{
int nLen = strlen(str);
int i;
for (i = 0; i < nLen; i++)
if ((str[i] >= 'A'&&str[i] <= 'Z') || (str[i] >= 'a'&&str[i] <= 'z'))
alph++;
else if (str[i] >= '0'&&str[i] <= '9')
num++;
else if (str[i] == ' ')
spa++;
else other++;
}
int len = strlen(str);
for (int i = 0; i <len; i++)