Compile a program to count the numbers, spaces, and the number of occurrences of all other characters in the input string.

Source: Internet
Author: User

Compile a program to count the numbers, spaces, and the number of occurrences of all other characters in the input string.

# Include <stdio. h> int main () {char a = 0; int num_count = 0; int space_count = 0; int other_count = 0; // note that a = getchar () cannot be written here (), then while (! = '\ N'). In this case, only one row can be input, and an endless loop while (a = getchar () is performed ())! = '\ N') {if (a> = '0' & a <= '9') {num_count ++;} else if (a = '') {space_count ++;} else {other_count ++;} printf ("num_count = % d \ n", num_count); printf ("space_count = % d \ n ", space_count); printf ("other_count = % d \ n", other_count); return 0;} another method ---- call the function: # include <stdio. h> # include <ctype. h> // checks the white space and calls the isspace () function. Therefore, you must call the int main () {char str [20] header file. // This field limits int num_count = 0; int space_count = 0; int other_count = 0; char * p = str; gets (str ); // receives the string while (* p) {if (* p> = '0' & * p <= '9') {num_count ++ ;} else if (isspace (* p) // use the isspace function to determine whether it is a blank character {space_count ++;} else {other_count ++;} p ++ ;} printf ("num_count = % d \ n", num_count); printf ("space_count = % d \ n", space_count); printf ("other_count = % d \ n ", other_count); return 0 ;}

 

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.