C language implementation -- & gt; statistics on the number of occurrences of each type in the input string

Source: Internet
Author: User

C Language --> statistics on the number of occurrences of each type in the input string
Problem description: compile a program to count the number, white space character, and the number of times all other characters appear in the input string. Code implementation:

# Include <stdio. h> # include <ctype. h> int main () {int other = 0; int space = 0; int a [10] = {0}; int I = 0; int ch = 0; while (ch = getchar ())! = EOF) {/* // isspace library function --- determines whether the input string ch is null. if yes, the if condition is true. */If (isspace (ch) {space ++;}/* // isdigit database function --- determines whether the input string ch is a number. if yes, the if condition is true. */Else if (isdigit (ch) {a [ch-'0'] ++;} else {other ++ ;}} printf ("space = % d \ n", space); for (I = 0; I <10; I ++) {printf ("% d: % d \ n ", I, a [I]);} printf (" other = % d \ n ", other );}

 

The result is:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.