Question

Write a C program that counts the number of each digit entered by the user (Input process should be ended using EOF character

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*C program that prompts user to enter a line of text
from keyboard and then press enter key then print
the characters in the text and its corresponding count
as stars on the console window.*/
//main.c
#include<stdio.h>
#include<conio.h>
//main function starts
int main()
{
   //declare an array to count each number
   int counter[10]={0,0,0,0,0,0,0,0,0,0};
   int c;
   int index;
   int i;

   //read each character until new line or end of line(^z)
   while (true)
   {
       c=getchar();
       if (c=='\n' || c==EOF)
           break;
       else
       {
           //switch case to count each corresponding number
           switch(c)
           {
           case '0':counter[0]++;break;
           case '1':counter[1]++;break;
           case '2':counter[2]++;break;
           case '3':counter[3]++;break;
           case '4':counter[4]++;break;
           case '5':counter[5]++;break;
           case '6':counter[6]++;break;
           case '7':counter[7]++;break;
           case '8':counter[8]++;break;
           case '9':counter[9]++;break;
           }
       }
   }
   //print the histogram of starts
   for(index=0;index<10;index++)
   {
       printf("%d",index);
       for(i=0;i<counter[index];i++)
           printf("%c",'*');
       printf("\n");
   }

   /*pause program output on console
   until user enters any key on keyboard*/
   getch();
   return 0;
}

-----------------------------Sample Ouptut1-------------------------------------------

182183428500560855599 10*** 1秒 2** 134 4* ***** 6* 7 8**** 9

-----------------------------Sample Ouptut2-------------------------------------------

77777445521056 104 1* 2* 3 4** 5*** 6 7***** 8 6

-----------------------------Sample Ouptut3-------------------------------------------

782222222315677999880001332092555 10米 1 2******* 3*** 4 5**** |6 7 |8林 19****

Add a comment
Know the answer?
Add Answer to:
Write a C program that counts the number of each digit entered by the user (Input...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Digit to WordsWrite a C program that asks the user for a two-digit number, then prints...

    Digit to WordsWrite a C program that asks the user for a two-digit number, then prints the English wordfor the number. Your program should loop until the user wants to quit.Example:Enter a two-digit number: 45You entered the number forty-five.Hint: Break the number into two digits. Use one switch statement to print the word for the firstdigit (“twenty,” “thirty,” and so forth). Use a second switch statement to print the word for thesecond digit. Don’t forget that the numbers between 11...

  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

  • Write a program in C++ to read a large piece of text from the user (multiple...

    Write a program in C++ to read a large piece of text from the user (multiple lines) until the user inputs EOF. Assume the input only contains alphabets, numbers, commas and periods. The program should first compute the number of occurrences of each character in the input. Next, the program displays a sorted list (in descending order) of each character and the corresponding number of occurrences of that character. The sorting should be performed on the counts and not the...

  • build a phone number from digits entered by your user. Your program will loop until 10...

    build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and 9 inclusively. If so, the digit will be stored as the next number in the...

  • c program that counts the number of characters, words and lines from standard input until EOF....

    c program that counts the number of characters, words and lines from standard input until EOF. attached is what i Have so far but its not working ?. about shell redirection Requirements 1. Write a C program that counts the number of characters, words and lines read from standard Input until EOF Is reached. 2. Assume the Input is ASCII text of any length. 3. Every byte read from stdin counts as a character except EOF 4. Words are defined...

  • Write a C++ program that inputs a single letter and prints out the corresponding digit on the...

    Write a C++ program that inputs a single letter and prints out the corresponding digit on the telephone.The letters and digits on a telephone are grouped this way:2 = ABC 4 = GHI 6 = MNO 8 = TUV3 = DEF 5 = JKL 7 = PRS 9 = WXYNo digits correspond to either Q or Z. For these two letters, your program should print a messageindicating that they are not used on a telephone. If a letter in lower...

  • Write a program that asks the user to input a 4-digit integer and then prints the...

    Write a program that asks the user to input a 4-digit integer and then prints the integer in reverse. Your program needs to implement a function reverse that will take in as input the 4-digit number and print it out in reverse. Your code will consist of two files: main.s and reverse.s. Main.s will ask for the user input number and call the function reverse. For ARM/Data Structure needs to be able to run on Pi Reverse.s will take in...

  • java Write an application that input a number from the user and checks if all digits...

    java Write an application that input a number from the user and checks if all digits are prime numbers using method prime. The number entered can be of any size. If all digits are prime your program should stop. Use do/while for reading the input and any loop format to test if the number is prime. When checking the prime numbers don’t use an if to check numbers from 1 – 9; you need to find an algorithm to check...

  • please I need help with the question: Problem: Write a C++ program that reads each character...

    please I need help with the question: Problem: Write a C++ program that reads each character from a user input, extracts the character if it is a digit, and calculates the sum of all the digits. The program stops reading the user input when the new line character ‘\n’ is encountered. For the output of the program, you should print each digit extracted from the user input and the sum of all the digits. (*The main difference between “cin >>...

  • Please write a java program that takes a large, user-entered number (usually 10 digits) and tallies...

    Please write a java program that takes a large, user-entered number (usually 10 digits) and tallies the 10 digits to be used in an array. Then, the method should tell the user how many of each number was input. For example, for the number 445903218, the program should say "2 4's, 1 5...," etc etc.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT