count digits, white space and others


/ Published in: C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #include <stdio.h>
  2.  
  3. // count the digit, white space, and others
  4. int main()
  5. {
  6. char c,nwhite,nother;
  7.  
  8. int ndigit[10];
  9.  
  10. nwhite = 0;
  11. nother = 0;
  12.  
  13. for(int i = 0; i < 10; i++)
  14. ndigit[i] = 0;
  15.  
  16. while( (c = getchar()) != EOF ) {
  17. if ( c >= '0' && c <= '9' )
  18. ++ndigit[c-'0'];
  19. else if ( c == '\n' || c == '\t' || c == ' ')
  20. ++nwhite;
  21. else
  22. ++nother;
  23. }
  24.  
  25. printf("The number of white space is: %d\n",nwhite);
  26. printf("The number of other char is: %d\n",nother);
  27.  
  28. int j = 9;
  29. while( j !=0 ){
  30. printf("The number of digit %d is",j);
  31. printf("%d\n",ndigit[j]);
  32. --j;
  33. }
  34. return 0;
  35. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.