C中的字符串中的大写字母和小写字母的计数数

编程入门 行业动态 更新时间:2024-10-27 14:19:19
本文介绍了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中的字符串中的大写字母和小写字母的计数数

本文发布于:2023-11-12 11:31:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581384.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大写字母   数数   字符串   字母

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!