C PROGRAM DOCUMENT EACH LINE OF CODE You learned getchar() and EOF in the class.
ID: 3787136 • Letter: C
Question
C PROGRAM DOCUMENT EACH LINE OF CODE
You learned getchar() and EOF in the class. You use them in an example to count the number of digits from input^1.Extend^2 that C program, so it will count the number of various individual English alphabets (both lowercase and uppercase). Also, count the total number of characters. You program should prints these: count of each individual digit (0-9) count of each individual alphabet letter (a-z and A-Z) total count of any kind of characters So basically the output will be the values stored in (10 + 26 + 26 + 1 =) 63 counter. Count the number of words (i.e., terms that are separated by whitespaces: space, tab, newline). Note that there could be more than one whitespaces between two terms.Explanation / Answer
#include <stdio.h>
#define MAX_SIZE 200
int main()
{
char input[MAX_SIZE];
int alphabets, digits, totalcharacters, others, terms;
alphabets = digits = totalcharactes = others = terms = 0;
int state = 0;
printf("Enter input : ");
scanf("%[^ ]",input);
for(int i=0; input[i] != '' ++i)
{
if(input[i] == ' ' || input[i] == ' ' || input[i] == ' ')
{
others++;
state = 0;
}
else if(state == 0)
{
state = 1;
terms++;
if((input[i] >= 'a' && input[i] <= 'z') || (input[i] >= 'A' && input[i] <= 'Z')) {
alphabets++;
}
else if(input[i] >= '0' && input[i] <='9') {
digits++;
}
else { others ++ ; }
}
}
totalcharacters = alphabets + digits + others;
printf("Alphabets = %d ", alphabets);
printf("Digits = %d ", digits);
printf("Total characters = %d ", totalcharacters);
printf("Total terms seprated by spaces = %d ", terms);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.