Write a program statistic in the input string: numbers, whitespace characters, and the number of occurrences of all other characters

Source: Internet
Author: User

#include  <stdio.h>int main () {     char a=0;     int num_count=0;    int space_count=0;    int  other_count=0;                                                   //note Here, cannot be written A=getchar (), then while (a!= ' \ n '), so that only one line is entered, and then a dead Loop      while ((A=getchar ())! = ' \ 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----Calling the function: #include  <stdio.h> #include  <ctype.h>    //the white-space character, Call the Isspace () function, so call the header file Int main () {   char str[20]; &nbsp   //this block has a limit on the input    int num_count=0;   int space_count=0 ;    int other_count=0;   char *p=str;   gets (str);    //Receive string    while (*p)    {     if (*p> = ' 0 ' &&*p<= ' 9 ')      {        num_ Count++;     }     else if (Isspace (*p))      //Use the Isspace function to determine if 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;} 


This article from "Thanksgiving" blog, reproduced please contact the author!

Write a program statistic in the input string: numbers, whitespace characters, and the number of occurrences of all other characters

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.