Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In your role as programmer, write a program that counts the numerals (\'0;, \'1\

ID: 3754447 • Letter: I

Question

In your role as programmer, write a program that counts the numerals ('0;, '1', .., '9') contained in 30 consecutive keyboard characters that are typed in by the user (after being prompted) Use getchar) to read each subsequent character. Your program should use a switch statement inside a for-loop and, then, print a report. If the typed character is not a numeral, default to counting nothing. Each switch case should address two simple statements, the first one simply increments the appropriate counter and the second one is a break. So, place the break on same line (after a tab) as the statement. While this is not "good style'", I think it makes the listing neater and more concise. In your role as user, type in more than 30 key-strokes after the prompt. Your program should end, and print its report, after the 30th, and ignore all others.

Explanation / Answer

Below is the solution:

#include<iostream>
using namespace std;

#define CHARACTER 80
// Driver function
int main()
{
char buffer[CHARACTER+1]; //buffer for charcter store                                                      
int i,number=0; //i for the loop and number for the count input number                                                                  
int ch;                                                                
                                                                              
cout<<"Please enter string: ";                                          
for ( i = 0; ( i < 30 ) && (( ch = getchar()) != EOF) && ( ch !=' ' ); ++i ){ //loop will run 30 times to count each string in nubber
   buffer[i] = ch; //store the inputed charcter number in buffer variable
   switch(buffer[i]){ //check for the input number
       //chcek if the number is between 0-9 count the number
           case '0':   number++;   break;
           case '1':   number++;   break;
           case '2':   number++;   break;
           case '3':   number++;   break;
           case '4':   number++;   break;
           case '5':   number++;   break;
           case '6':   number++;   break;
           case '7':   number++;   break;
           case '8':   number++;   break;
           case '9':   number++;   break;
       }
}                                          
                                                               
    buffer[i] = ''; // a string should always end with ''
    cout << "Number typed is: " << number << endl;
    //cout << "string : " << buffer << endl;
    return 0;
}


sample output:
Please enter string: #Nehal01fOr@Ahmed107Arwal3BiharlkIndiajs5
Number typed is: 6