Posted By

sebastianf on 01/31/10


Tagged

count getchar digits


Versions (?)

count digits


 / Published in: C
 

  1. /* count digits and others */#include <stdio.h>
  2.  
  3. int main (int argc, const char * argv[]){
  4. int c, i, nwhite, nother;
  5. int ndigit[10];
  6.  
  7. nwhite=nother=0;
  8.  
  9. for(i=0; i<10; i++)
  10. ndigit[i]=0;
  11.  
  12.  
  13. while((c=getchar())!=EOF)
  14. if(c>='0' && c<='9')
  15. ++ndigit[c-'0'];
  16. elseif(c==' '||c=='\n'||c=='\t')
  17. ++nwhite;
  18. else
  19. ++nother;
  20.  
  21.  
  22. printf("digits =");
  23. for(i=0; i<10; ++i)
  24. printf(" %i", ndigit[i]);
  25. printf("\nwhite space = %i, other = %i\n", nwhite, nother);
  26. }

Report this snippet  

You need to login to post a comment.