In: Computer Science
(b) You will write a program that will do the following: prompt the user enter characters from the keyboard, you will read the characters until reading the letter ‘Q’ You will compute statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character ‘Q’.
Example input mJ0*5/]+x1@3qcxQ
The ‘Q’ should be included when computing the statistics properties of the input. Since characters are integers you will determine the average character (average when one looks at the character as an integer (its ASCII value). To achieve this you will need to compute the sum of the ASCII values of the characters. We are interested in determining the following statistics
1.The number of lowercase alphabetical characters
2.The number of even digits ‘0’, ‘2’, ‘4’, ‘6’, ‘8’
3.The total number of characters inputted (include ‘Q’)
4.The number of line feeds (ASCII value of 10)
5.The average character (here we treat the characters as an integer based on their ASCII values)
6.The average alphabetical character that is read (include the ‘Q’)
Output each in a formatted manner (in your output describe what the output is,
for example The number of even digits is 5
THIS CODE HAS TO BE WRITTEN USING THE C PROGRAMMING LANGUAGE.
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.
Thank You!
===========================================================================
#include<stdio.h>
int main(){
int lowercases = 0,
evendigits =0,
total_chars =0,
line_feeds =0,
total_sum =0,
alphabetical_sum = 0,
total_alphabets = 0;
char input;
printf("Enter a character (Q to
exit): ");
while(input!='Q')
{
input =
getchar();
if(input==10)
line_feeds+=1;
else
if('a'<=input && input<='z'){
lowercases+=1;
total_alphabets+=1;
alphabetical_sum+= int(input);
} else
if('A'<=input && input<='Z'){
total_alphabets+=1;
alphabetical_sum+= int(input);
}
else
if(input=='0' || input=='2' || input=='4' || input=='8') {
evendigits+=1;
}
total_sum +=
int(input);
total_chars+=1;
}
//1.The number of lowercase
alphabetical characters
printf("Total number of lowercases:
%d\n",lowercases);
//2.The number of even digits ‘0’,
‘2’, ‘4’, ‘6’, ‘8’
printf("Even digits:
%d\n",evendigits);
//3.The total number of characters
inputted (include ‘Q’)
printf("Total number of characters:
%d\n",total_chars);
//4.The number of line feeds (ASCII
value of 10)
printf("Line feeds entered:
%d\n",line_feeds);
//5.The average character
printf("Average Character:
%d\n",total_sum/total_chars);
//6.The average alphabetical
character that is read (include the ‘Q’)
printf("Average alphabetical
character: %d\n",alphabetical_sum/total_alphabets);
}