| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 522 人关注过本帖
标题:[求助]计数问题
只看楼主 加入收藏
sunpy
Rank: 1
来 自:厦门
等 级:新手上路
帖 子:118
专家分:0
注 册:2007-10-1
结帖率:100%
收藏
 问题点数:0 回复次数:6 
[求助]计数问题
下面这段程序运行时会出现乱码,不知道问什么?


//宏定义
#include<stdio.h>
#define MAXLEN 100
//定义全局变量
int nchar,ndigit,nspace,nother;
//定义函数
int count(char s[MAXLEN]);
//主函数
int
main()
{
char a[MAXLEN];
//要求输入字符串
printf("please input a string:\n");
scanf("%s",a);

//调用count函数
count(a);

//输出结果
printf(" nchar=%d,\n ndigit=%d,\n nspace=%d,\n nother=%d\n",nchar,ndigit,nspace,nother);
return(0);
}
/*count 函数*/
int
count( char s[MAXLEN] )
{
int j;
char c;
nchar=ndigit=nspace=nother=0;
for(j=0;j<MAXLEN&&s[j]!='\n';j++)
{
c=s[j];
if(c>='A'&&c<='Z')
nchar++;
else if( c>='a'&&c<='z')
nchar++;
else if(c>='0'&&c<='9')
ndigit++;
else if(c==' ')
nspace++;
else
nother++;
}
return(0);
}

搜索更多相关主题的帖子: 计数 
2007-11-11 13:54
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
很正常啊.

倚天照海花无数,流水高山心自知。
2007-11-11 14:08
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 

哪有什么乱码?
please input a string:
dsa32e 23rwed324eddas21345@#$%^&*hbsads4546 $%^fghgh56$%^g
nchar=15,
ndigit=4,
nspace=6,
nother=75
Press any key to continue...


倚天照海花无数,流水高山心自知。
2007-11-11 14:09
haha188
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-10-26
收藏
得分:0 

和你的编译器有关吧

2007-11-11 14:11
sunpy
Rank: 1
来 自:厦门
等 级:新手上路
帖 子:118
专家分:0
注 册:2007-10-1
收藏
得分:0 
好像都和我数的结果不一样

please input a string:
2656666666
nchar=12,
ndigit=13,
nspace=2,
nother=73


Terminated with return code 0
Press any key to continue ...
我输入的全是数字,结果中的nchar应该等于0.。。。。。


荀子《劝学》:“不积跬步,无以至千里;不积小流,无以成江海.”
2007-11-11 16:26
可见光
Rank: 1
等 级:新手上路
帖 子:143
专家分:0
注 册:2007-6-15
收藏
得分:0 
#include<stdio.h>
#define MAXLEN 100
//定义全局变量
int nchar,ndigit,nspace,nother;
//定义函数
int count(char s[MAXLEN]);
//主函数
int
main()
{
char a[MAXLEN];
//要求输入字符串
printf("please input a string:\n");
scanf("%s",a);

//调用count函数
count(a);

//输出结果
printf(" nchar=%d,\n ndigit=%d,\n nspace=%d,\n nother=%d\n",nchar,ndigit,nspace,nother);
return(0);
}
/*count 函数*/
int
count( char s[MAXLEN] )
{
int j;
char c;
nchar=ndigit=nspace=nother=0;
for(j=0;j<MAXLEN&&s[j]!='\n';j++)
{
c=s[j];
if(c>='A'&&c<='Z')
nchar++;
else if( c>='a'&&c<='z')
nchar++;
else if(c>='0'&&c<='9')
ndigit++;
else if(c==' ')
nspace++;
else
nother++;
}
return(0);
}

定义MAXLEN 为100,输出的4个数的和就是100了。。。
2007-11-11 19:06
now
Rank: 1
来 自:广州
等 级:新手上路
帖 子:544
专家分:0
注 册:2007-11-9
收藏
得分:0 

please input a string:
2656666666
nchar=0,
ndigit=10,
nspace=0,
nother=90
Press any key to continue ...
很正常啊。。。。


GIS
Geographic Information System
你在哪里?——》你的坐标?
2007-11-11 19:18
快速回复:[求助]计数问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015260 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved