C中的字符串中的大写字母和小写字母的计数数 [英] Count Number of Uppercase and Lowercase Letters in a string in C

查看:224
本文介绍了C中的字符串中的大写字母和小写字母的计数数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个用于查找大写和小写字母数的代码。例如

I want to create a code that finds the number of Uppercase and Lowercase letters. So for example

input:
HelloGUYS
Output:
4 5

我拥有的代码是:

#include <stdio.h>
#include <string.h>

int main()
{
int i, longitud;
int X[26];
char line[1010];

for(i=0; i<26; i++)
{
    X[i] = 0;
}

while(gets(line))
{
   longitud = strlen(line);

   for(i=0; i<longitud; i++)
   {
      if(line[i] >= 'a' && line[i] <= 'z')
      {
        X[line[i] - 'a']++;
      }
      else if(line[i] >= 'A' && line[i] <= 'Z')
      {
        X[line[i] - 'A']++;
      }
   }
}

for(i=0; i<26; i++)
{
    printf("%c: %d\n", i+'a' , X[i]);
}

return 0;
}

事情是我的程序不工作,我不知道什么是问题。我真的aprreciate的帮助。感谢:)

The thing is that my program does not work, I don't know what is the problem. I really aprreciate the help. Thanks :)

推荐答案

您需要的是比这更简单的方法。

What you need is way simpler than this.

您需要 #include< ctype.h> ,只需使用 isupper() islower()

跳过输入的每个字符并有两个计数器 - upper_counter lower_counter

Go over each char of input and have 2 counters - upper_counter, lower_counter.

这篇关于C中的字符串中的大写字母和小写字母的计数数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆