统计字符串中字母、数字、空格数目

Smilingmm 2018-07-19 03:28:34
感觉自己的程序没有什么问题,但是运行的结果输出所有参数都是0

#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("%s", 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; 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++;


}

...全文
490 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zangfong 2018-07-20
  • 打赏
  • 举报
回复
引用 10 楼 Schlangemm 的回复:
已经找到了自己原来程序的错误,看久了就会头脑不清楚
恭喜恭喜,加油加油!
zangfong 2018-07-20
  • 打赏
  • 举报
回复
引用 9 楼 Schlangemm 的回复:
[quote=引用 5 楼 zangfong 的回复:]
[quote=引用 4 楼 Schlangemm 的回复:]
你好,我想问一下,我用VS2017用不了gets函数,有其他替代的函数吗

试试
fgets(text,sizeof(text),stdin);

或者
gets_s(text,sizeof(text)) 
[/quote]
用gets函数会出现警告,但是程序还是能正常运行,我想请问一下这个有关系吗;还要那个sizeof(text)就是直接可以用的函数吗,我看的c语言的书里使用strlen函数来测得字符数组的实际长度,这两个有什么区别吗
[/quote]出现警告在本题中肯定是没啥关系,但是用gets不太安全,所以编译时会给出警告,可以用fgets取代。具体请百度gets与fgets,可以找个自己看得懂的解释。
sizeof是关键字,直接可用,返回的是一个对象或者类型所占的内存字节数,所以本题中sizeof(text) = sizeof(char) * 80 = 1 * 80 =80;而strlen是函数,返回的是text字符串的长度(碰到第一个字符串结束符'\0'为止的长度)。举例,如果你输入了“12345”,sizeof(text)还是80, strlen(text) = 5。
Smilingmm 2018-07-20
  • 打赏
  • 举报
回复
已经找到了自己原来程序的错误,看久了就会头脑不清楚
Smilingmm 2018-07-20
  • 打赏
  • 举报
回复
引用 5 楼 zangfong 的回复:
[quote=引用 4 楼 Schlangemm 的回复:]
你好,我想问一下,我用VS2017用不了gets函数,有其他替代的函数吗

试试
fgets(text,sizeof(text),stdin);

或者
gets_s(text,sizeof(text)) 
[/quote]
用gets函数会出现警告,但是程序还是能正常运行,我想请问一下这个有关系吗;还要那个sizeof(text)就是直接可以用的函数吗,我看的c语言的书里使用strlen函数来测得字符数组的实际长度,这两个有什么区别吗
kfyniriu1 2018-07-19
  • 打赏
  • 举报
回复
6楼写得匆忙,重新对最后一个代码补充
char a=getchar()
int i=0;
while(a!='\n')
{
str[i]=a;
i++;
a=getchar();
}
str[i]='\0';

最后字符串需要加上结束符
自信男孩 2018-07-19
  • 打赏
  • 举报
回复
for (i = 0; i != '\0'; i++)

这样的设计循环一次也不会执行,因为'\0'就等于0;当i= 0;时,i != '\0'就是假
改成:
for (i = 0; str[i] != '\0'; i++)
kfyniriu1 2018-07-19
  • 打赏
  • 举报
回复
程序主要问题出在 for (i = 0; i != '\0'; i++) 循环条件 i!='\0' 处。

这个条件是不成立的,可以修改为 str[i]!='\0'

或者添加头文件#include<string.h> 使用strlen函数 for语句前添加:int j=strlen(str); 将 i!='\0' 替换成 i<strlen

在或者用while语句
int i=0;
while(str[i]!='\0')
{
......(for语句里面的内容)
i++;
}

以上可以解决for循环处的问题。

scanf("%s", text); 是不能读取到空格键的,需要读取空格键,可以修改为:scanf("%[^\n]", text); 或者 gets(text);
再或者:
char a=getchar()
int i=0;
while(a!='\n')
{
str[i]=a;
i++;
a=getchar();
}
zangfong 2018-07-19
  • 打赏
  • 举报
回复
引用 4 楼 Schlangemm 的回复:
你好,我想问一下,我用VS2017用不了gets函数,有其他替代的函数吗

试试
fgets(text,sizeof(text),stdin);

或者
gets_s(text,sizeof(text)) 
Smilingmm 2018-07-19
  • 打赏
  • 举报
回复
你好,我想问一下,我用VS2017用不了gets函数,有其他替代的函数吗
zangfong 2018-07-19
  • 打赏
  • 举报
回复
你的运行的结果输出所有参数都是0,这是由你的for循环引起的
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++;
}
sghcpt 2018-07-19
  • 打赏
  • 举报
回复
楼主:改为下面的代码试试:
#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++;
}


网上说scanf或者scanf_s函数如果遇到输入字符串中存在空格时,会认为字符串输入已经结束,所以如果你上面输入空格字符的,那么你给出的代码会只能接收到空格字符前面的字符,所以需要使用“%[^\n]”这样的输入格式才可以接收到空格字符的输入。
yiyefangzhou24 2018-07-19
  • 打赏
  • 举报
回复
循环处改成
int len = strlen(str);
for (int i = 0; i <len; i++)

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧