【问题标题】:Printing lowercase, uppercase, and number of numbers打印小写、大写和数字个数
【发布时间】:2018-10-20 09:24:14
【问题描述】:
#include<stdio.h>

int main() { 
   char text[1000];
   int ch;
   int index = 0;

   while ((ch = getchar()) != EOF) {
      text[index] = ch;
      index++;
   }
   text[index] = '\0';

   int i =0;
   int num_Count=0; 
   int lower_Count=0; 
   int upper_Count =0;

   while(i < index) {
    if((text[i]>='0') && (text[i]<='9')){
        num_Count ++;
        i++;
    }
    else if((text[i]>='A') && (text[i]<='Z')){
        upper_Count++;
        i++;
    }
    else if((text[i]>='a') && (text[i] <='z')){
        lower_Count++;
        i++;
    }
    else
        i++;
}
printf("%d %d %d", num_Count, lower_Count, upper_Count);
return 0;
}

是一个程序,在输入句子时输出小写、大写和数字的个数。 例如,

Hi
Name
100 

将输出3 4 2

我不断看到运行时错误。 (while) 部分似乎是错误的。我不知道出了什么问题。

【问题讨论】:

标签: c uppercase lowercase


【解决方案1】:

我在我的系统中运行了您的代码并检查了输入:Hi Name 100。我得到的输出是3 4 2,这是预期的输出。我觉得代码可以在无限循环中运行的唯一地方是在读取输入时。尝试将 ctrl+ d 用于EOFctrl+ z 用于 windows。

休息一下就好了。

【讨论】:

    【解决方案2】:

    EOF 表示文件结束。当您从文件中读取数据时使用它。我建议放一个像换行符('\n')这样的字符。

    【讨论】:

    • 按 Ctrl-D 也会设置 EOF
    猜你喜欢
    • 2020-12-24
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多