【问题标题】:Program that counts Uppercase and Lowercase letters in a String in C计算C中字符串中大写和小写字母的程序
【发布时间】:2015-10-27 23:56:45
【问题描述】:

所以我想制作一个代码,您可以在其中找到字符串中大小写字母的数量(无空格) 所以我想要这样的东西:

input:
HEllO
Output:
2 3

所以我的代码是这样的:

#include<stdio.h>
int main() {
int upper = 0, lower = 0;
char ch[80];
int i;

printf("\nEnter The String : ");
gets(ch);

i = 0;
while (ch[i] != '') {
  if (ch[i] >= 'A' && ch[i] <= 'Z')
     upper++;
  if (ch[i] >= 'a' && ch[i] <= 'z')
     lower++;
  i++;
  }

  printf("%d %d", upper, lower);


  return (0);
  }

代码有问题,但我找不到错误。有人可以修复它吗?谢谢。

【问题讨论】:

  • while (ch[i] != '') 应该是 while (ch[i] != '\0')
  • 还有什么问题?顺便说一句,'''\0' 的同义词?
  • @luk32 不,'' 不是 '\0' 的同义词。这是语法错误,无法编译。
  • 这不是您的问题的原因,但请注意您应该永远使用gets()。它不会传递缓冲区的长度,因此如果输入长度超过 79 个字符,它将溢出 ch 数组,导致未定义的行为。相反,使用 fgets(ch, sizeof(ch), stdin);请注意,与 get 不同,fgets 将在缓冲区中包含换行符(如果存在),但这对您的情况没有影响,因为 '\n' 不在 [A-Z] 或 [a-z] 中。

标签: c arrays string char int


【解决方案1】:

更正的代码-

#include <stdio.h>

int main(void)
{
    int upper = 0, lower = 0;
    char ch[80];
    int i = 0;
    printf("\nEnter The String : ");
    fgets(ch, sizeof(ch), stdin);
    while (ch[i] != '\0')
    {
        if (ch[i] >= 'A' && ch[i] <= 'Z')
            upper++;
        if (ch[i] >= 'a' && ch[i] <= 'z')
            lower++;
        i++;
    }
    printf("\nuppercase letter(s): %d \nlowercase letter(s): %d", upper, lower);
    return 0;
}

注意:我使用了fgets() 而不是gets(),因为后者存在缓冲区溢出问题。

【讨论】:

    【解决方案2】:

    在 C 中,字符串总是以'\0' 结尾。空字符串'' 和转义空字符不相同

    while (ch[i] != '') 应该是while (ch[i] != '\0')

    你的程序应该可以工作了。

    【讨论】:

      【解决方案3】:

      问题是表达式''。字符常量必须在单引号之间包含一些内容。在这种情况下,您要测试字符串的结尾,因此您将使用空字符常量:'\0'。

      #include <stdio.h>
      
      int main(void) {
          int upper = 0, lower = 0;
          char ch[80];
          int i;
      
          printf("\nEnter The String : ");
      
          fgets(ch, sizeof(ch), stdin);
          i = 0;
          while (ch[i] != '\0') {
              if (ch[i] >= 'A' && ch[i] <= 'Z')
                  upper++;
              if (ch[i] >= 'a' && ch[i] <= 'z')
                  lower++;
              i++;
          }
      
          printf("%d %d\n", upper, lower);
      
          return 0;
      }
      

      请注意,我还将gets 替换为fgets。你永远不应该使用gets()。它不会传递缓冲区的长度,因此如果输入长度超过 79 个字符,它将溢出 ch 数组,导致未定义的行为。 fgets 接受一个 size 参数并在读取 size - 1 后停止读取。如果输入中存在换行符,它还会在结果字符串中包含换行符,而输入中没有。

      一种适用于所有输入长度的更好方法是一次读取一个字符的字符串,而不必费心存储它,因为您只关心上限和下限的计数。

      #include <stdio.h>
      
      int main(void) {
          unsigned upper = 0, lower = 0;
          printf("\nEnter The String : ");
      
          int c;
          while (EOF != (c = getchar())) {
              if ('\n' == c) break;
      
              if (c >= 'A' && c <= 'Z')
                  upper++;
              if (c >= 'a' && c <= 'z')
                  lower++;
          }
          printf("%u %u\n", upper, lower);
      
          return 0;
      }   
      

      【讨论】:

        猜你喜欢
        • 2020-01-04
        • 2014-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-11
        • 2017-03-05
        • 2021-01-06
        相关资源
        最近更新 更多