Reputation: 9
#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;
}
It is a program that outputs the number of lower case, upper case, and number when the sentence is inputted. For example,
Hi
Name
100
Would output 3 4 2
I keep seeing a runtime error. The (while) part seems to be wrong.. I do not know what's wrong.
Upvotes: 0
Views: 198
Reputation: 95
EOF means End Of File. It is used when you read data from a file. I suggest put a character like newline ('\n').
Upvotes: 0
Reputation: 896
I ran your code in my system and checked for the input: Hi Name 100
. The output I got is 3 4 2
which is the expected output. I feel the only place where the code can run in an infinite loop is while reading the inputs. Try to use ctrl+ d for EOF
or ctrl+ z for windows.
Rest every thing is fine.
Upvotes: 1