C practical exercises topic 17

Time:2020-10-18

Title:Input a line of characters and count the number of English letters, spaces, numbers and other characters.

Program analysis:Using the while statement, the condition is that the input character is not ‘\ n’.

example:

#include
 int main()
 {
     char c;
     int letters=0,spaces=0,digits=0,others=0;
     Printf ("please enter some letters:;
     while((c=getchar())!='\n')
     {
         if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
             letters++;
         else if(c>='0'&&c<='9')
             digits++;
         else if(c==' ')
             spaces++;
         else
             others++;
     }
     Printf ("letter =% D, number =% D, space =% D, other =% D / N", letters, digits, spaces, others);
     return 0;
 }

The output results of the above examples are as follows:

Please enter some letters:
www.kangyifan.com 123
Letter = 15, number = 3, space = 1, others = 2

Thank you for your reading, please feel with your heart! Hope to help you who love learning!! Sharing is also a kind of happiness!!! Please relay…

Click to view the original text, thank you!