鱼C论坛

 找回密码
 立即注册
查看: 4850|回复: 12

急求编写一个关于数组的小程序,C语言

[复制链接]
发表于 2013-5-24 15:37:16 | 显示全部楼层 |阅读模式
10鱼币
采用二维数组存储一篇文章,它共有3行,每行最多有80个字符。要求分别统计出其中英文大写字母、小写字母、数字、空格以及其它字符的个数。
测试数据:   Hello Jack, what time is it? It is 16:50.
                    Thisis a array test progam.
                    Ihave finished it, bye-bye.

最佳答案

查看完整内容

这样行不。。。。 结果这样:
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-5-24 15:37:17 | 显示全部楼层
这样行不。。。。
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #define HLEN 3
  4. #define WLEN 80
  5. int main()
  6. {
  7.         char input_str[HLEN][WLEN];
  8.         int i, j, number = 0, upper_str = 0, lower_str = 0, space_str = 0, other_str = 0;
  9.         for(i = 0; i < HLEN; i++)//每次只能输入三行
  10.                 gets(input_str[i]);
  11.         for(i = 0; i < HLEN; ++i)
  12.         {
  13.                 if(input_str[i] == NULL)
  14.                         break;
  15.                 for(j = 0; j < WLEN; ++j)
  16.                 {
  17.                         if(input_str[i][j] == NULL)        
  18.                                 break;
  19.                         else
  20.                         {
  21.                                 if(isalnum(input_str[i][j]))//数字或字母
  22.                                 {
  23.                                         if(isdigit(input_str[i][j]))//数字
  24.                                                 number++;
  25.                                         else if(isupper(input_str[i][j]))//大写
  26.                                                 upper_str++;
  27.                                         else        //小写
  28.                                                 lower_str++;                              
  29.                                 }
  30.                                 else if(isspace(input_str[i][j]))//空格
  31.                                         space_str++;
  32.                                 else        //其他字符
  33.                                         other_str++;
  34.                         }
  35.                 }
  36.         }
  37.         printf("\n数字字符个数为: %d\n", number);
  38.         printf("大写字符个数为: %d\n", upper_str);
  39.         printf("小写字符个数为: %d\n", lower_str);
  40.         printf("空格字符个数为: %d\n", space_str);
  41.         printf("其他字符个数为: %d\n", other_str);        
  42.         return 0;
  43. }
复制代码


结果这样:
在.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-5-24 16:29:45 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. #define R 3
  5. #define L 100

  6. int main(void)
  7. {
  8.         char buff[R][L];
  9.         int *p = NULL;
  10.         int i,j;
  11.         int ch_num=0; //数字字符个数
  12.         int ch_max=0;  //大写字符个数
  13.         int ch_min=0;  //小写字符个数
  14.         int ch_null=0; //空格字符个数
  15.         int ch_other=0;//其他字符个数
  16.        
  17.         for (i=0; i<R; ++i)  //输入字符串
  18.         {
  19.                 printf("请输入%d个字符串:", i+1);
  20.                 fgets(buff[i], L, stdin);
  21.         }

  22.         for (i=0; i<R; ++i)
  23.         {
  24.                 for (j=0; j<strlen(buff[i]); ++j)
  25.                 {
  26.                         if (buff[i][j]>='0' && buff[i][j]<='9')
  27.                         {
  28.                                 ++ch_num;
  29.                         }
  30.                         else if (buff[i][j] >='A' && buff[i][j]<='Z')
  31.                         {
  32.                                 ++ch_max;
  33.                         }
  34.                         else if (buff[i][j] >= 'a' && buff[i][j]<='z')
  35.                         {
  36.                                 ++ch_min;
  37.                         }
  38.                         else if (' ' == buff[i][j])
  39.                         {
  40.                                 ++ch_null;
  41.                         }
  42.                         else
  43.                         {
  44.                                 ++ch_other;
  45.                         }
  46.                 }
  47.         }

  48.         printf("字符串当中数字字符,大写字符,小写字符,空格字符和其他字符个数分别为:%d,%d,%d,%d,%d\n",
  49.                 ch_num, ch_max, ch_min, ch_null, ch_other);
  50.         return 0;
  51. }
复制代码
代码仅供参考,希望对楼主有用
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-5-24 16:49:42 | 显示全部楼层
写了个函数如下,直接调用即可。
  1. void _func()
  2. {
  3.         char cString[3][81] = {0};

  4.         unsigned int nSpaceCount = 0;     //空格的个数
  5.         unsigned int nCapsWordCount = 0;  //大写字母个数
  6.         unsigned int nLowerWordCount = 0; //小写字母个数
  7.         unsigned int nNumCount = 0;       //数字字符个数
  8.         unsigned int nOtherCount = 0;     //其他字符个数

  9.         int i = 0;
  10.         int j = 0;
  11.         for (i = 0; i < 3; i++)
  12.         {
  13.                 printf("请输入第%d行内容(每行不超过80字符):",i);

  14.                 gets(cString[i]);
  15.                
  16.                 for(j = 0; j < 81; j++)
  17.                 {
  18.                         //32是空格的ascii码
  19.                         if (cString[i][j] == ' ')
  20.                         {
  21.                                 nSpaceCount++;
  22.                         }
  23.                         else if (cString[i][j] >= 48 && 57 >= cString[i][j])
  24.                         {
  25.                                 nNumCount++;
  26.                         }
  27.                         else if (cString[i][j] >= 97 && 122 >= cString[i][j])
  28.                         {
  29.                                 nLowerWordCount++;
  30.                         }
  31.                         else if (cString[i][j] >=65 && 90>= cString[i][j])
  32.                         {
  33.                                 nCapsWordCount++;
  34.                         }
  35.                         else
  36.                         {
  37.                                 nOtherCount++;
  38.                         }
  39.                 }
  40.         }
  41.         printf("大写字母个数%d\n小写字母个数%d\n空格个数%d\n数字字符个数%d\n其他字符个数%d\n:",nCapsWordCount,nLowerWordCount,nSpaceCount,nNumCount,nOtherCount);
  42. }
复制代码
结果如下图
11111111.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-5-25 02:28:43 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-5-25 02:30:24 | 显示全部楼层
小亮1201 发表于 2013-5-24 16:29
代码仅供参考,希望对楼主有用

指针还没掌握
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-5-25 02:31:25 | 显示全部楼层
小亮1201 发表于 2013-5-24 16:29
代码仅供参考,希望对楼主有用

不过还是谢谢啦,辛苦啦
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-5-25 02:35:32 | 显示全部楼层
丝连 发表于 2013-5-25 02:31
不过还是谢谢啦,辛苦啦

#include<stdio.h>
void main()
{
        char str[3][80];
        int i,j,h=0,k=0,l=0,m=0,n=0;
                printf("请输入文章:\n");
        for(i=0;i<3;i++)
        {
                gets(str[i]);
                for(j=0;j<80;j++)
                {
                        if(str[i][j]>='a'&&str[i][80]<='z')
                                h++;
                        else if(str[i][j]>='A'&&str[i][j]<='Z')
                                k++;
                        else if(str[i][j]>='0'&&str[i][j]<='9')
                                l++;
                        else if(str[i][j]==' ')
                                m++;
                        else
                                n++;
                }
        }
        printf("小写字母=%d,大写字母=%d,数字=%d,空格=%d,其他=%d\n",h,k,l,m,n);
}
[img][/img]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-5-25 02:38:59 | 显示全部楼层
丝连 发表于 2013-5-25 02:35
#include
void main()
{

这是我后来写的,图片不知道怎么弄上来
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-6-3 18:18:38 | 显示全部楼层

强烈支持楼主ing……
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-6-3 18:22:30 | 显示全部楼层

强烈支持楼主ing……
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-6-3 21:20:04 | 显示全部楼层
学习学习。。。。。。。。。。。。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-6-4 11:55:03 | 显示全部楼层


强烈支持楼主ing……
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-27 23:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表