| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1399 人关注过本帖
标题:字符数组,怎么统计各字符的个数?
只看楼主 加入收藏
liqingyang
Rank: 2
等 级:论坛游民
帖 子:47
专家分:39
注 册:2010-2-24
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
字符数组,怎么统计各字符的个数?
/*有一篇文章,一共3行,每行80个字符,要求统计出其中的英文大小写字母、数字、空格、和其它字符的个数*/
#include    "stdio.h"
#include    "string.h"
void    main()
{
    char    str[3][80],tmp;
    int        DA,xiao,num,kong,aaa,i,j=0;
    DA=0;//大写字母个数
    xiao=0;//小写字母个数
    num=0;//数字个数
    kong=0;//空格数
    aaa=0;//其它个数

    for(i=0;i<3;i++)
        gets(str[i]);//输入这里写的不对,但不知道怎么写,求指点

    for(i=0;i<3;i++)
        for(j=0;j<strlen(str[i]);j++)
        {
            tmp=str[i][j];
            if(tmp==' ')
                kong++;
            if(tmp>64 && tmp<91)
                DA++;
            if(tmp>96 && tmp<123)
                xiao++;
            if(tmp>47 && tmp<58)
                num++;
            else
                aaa++;
        }
    printf("文中共有大写字母:%d\n小写字母:%d\n空格:%d\n数字:%d\n其它:%d\n",DA,xiao,kong,num,aaa);
}



最后输出结果的时候,那个其它统计的不对,是怎么回事?
for(j=0;j<strlen(str[i]);j++)这一句里为什么编译器提示‘<’有误?
这个程序有没有更好的写法,我憋了半天才写出来?



搜索更多相关主题的帖子: 字母 include 文章 
2012-06-05 23:53
oueryixiaxia
Rank: 2
等 级:论坛游民
帖 子:12
专家分:44
注 册:2012-5-21
收藏
得分:5 
程序代码:
#include <stdio.h>
void main()
{
    int letter, space, number, other;
    char ch;
    letter = space = number = other = 0;     //为变量赋值;
    printf("请输入字符串: ");
    while((ch = getchar()) != '\n')         //while循环
        if('A' <= ch && ch <= 'Z' || 'a' <= ch && ch <= 'z') letter++;        //if语句循环;
            else
        if(ch == ' ') space++;              //潜逃定义if语句
            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);
}
2012-06-07 00:30
ss1345252
Rank: 1
等 级:新手上路
帖 子:1
专家分:5
注 册:2012-6-7
收藏
得分:5 
#include<stdio.h>
#include<string.h>
int main()
{
    char str[3][80];
    int space,LETTER,letter,number,other,i,j;
    space=letter=number=other=LETTER=0;
    printf("请输入字符串:\n");
    for(i=0;i<3;i++)
        gets(str[i]);
    for(i=0;i<3;i++)
        for(j=0;j<strlen(str[i]);j++)
            {
                if('a'<=str[i][j]&&str[i][j]<='z')
                    letter++;
                else
                    if('A'<=str[i][j]&&str[i][j]<='Z')
                        LETTER++;
                    else
                        if('0'<=str[i][j]&&str[i][j]<='9')
                            number++;
                        else
                            if(str[i][j]==' ')
                                space++;
                            else
                                other++;

            }
    printf("大写字母:%d个\n小写字母:%d个\n空格:%d个\n数字:%d个\n其它字符:%d个\n",LETTER,letter,space,number,other);
    return 0;

}
楼主的问题出在else那儿,else里面包含了数字外的所有字符,所有其他不对!
2012-06-07 12:59
阿鞠尼
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:首尔
等 级:蒙面侠
威 望:5
帖 子:1467
专家分:4442
注 册:2012-5-30
收藏
得分:5 
回复 2楼 oueryixiaxia
为什么这个代码就能避免三楼说的问题呢

喜欢睡觉 却经常熬夜
2012-06-08 11:14
快速回复:字符数组,怎么统计各字符的个数?
数据加载中...
 
   



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

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