edited by
907 views
0 votes
0 votes
#include<studio.h>

main()

{        int c, i ,nwhite, nother;

          int ndigit[10];

          nwhite = nother = 0;

         for(i=0; i <10; ++i)

           ndigit[i]= 0;

        while((c= getchar())!=EOF)

              if (c>='0' && c<='9' )

                   ++ndigit[c-'0'];

             elseif ( c== ' ' ll c == ' /n' ll c=='/t ')

                  ++nwhite;

             else

                ++nother;

    printf("digits=");

   for(i= 0; i< 10; ++i)

        printf("%d" , ndigit[I]);

        printf(", white space =%d, other= %d, other =%d", nwhite , nother);

}
edited by

1 Answer

Best answer
2 votes
2 votes

Before posting any "code" please check if the syntax is correct. ( I am a stickler for good written codes. (#include<studio.h>!!! ))

Anyways, I'll dissect this example from K&R.

#include <stdio.h>
   void main()
   {
       int c, i, nwhite, nother;
       int ndigit[10];
       nwhite = nother = 0;
       for (i = 0; i < 10; ++i)
           ndigit[i] = 0;
       while((c = getchar()) != EOF)
           if(c >= '0' && c <= '9')
              ++ndigit[c-'0'];
           else if(c == ' ' || c == '\n' || c == '\t')
               ++nwhite;
           else
               ++nother;
       printf("digits =");
       for (i = 0; i < 10; ++i)
           printf(" %d", ndigit[i]);
       printf(", white space = %d, other = %d\n", nwhite, nother);
}

Let's dissect parts of this. 

for (i = 0; i < 10; ++i)
           ndigit[i] = 0;

Just set all the elements of the array as 0 and using an array it is easier to hold all the 10 digits rather than 10 variables. 

while((c = getchar()) != EOF)
           if(c >= '0' && c <= '9')
              ++ndigit[c-'0']; // implicit char to int conversion, rookie trick
           else if(c == ' ' || c == '\n' || c == '\t')
               ++nwhite;
           else
               ++nother;

Iterate the "text" or "string" or "file" ( user input ) till EOF is reached. While iterating check each character individually and the counter respective to the type in incremented.
(if it's a digit -> ndigit[that digit]++, any of \n, \t or whitespace you would increment nwhite, and if it's something else ( alphabets, special symbols) nother++ )

printf("digits =");
       for (i = 0; i < 10; ++i)
           printf(" %d", ndigit[i]);
       printf(", white space = %d, other = %d\n", nwhite, nother);

This piece of code just prints out the result. 

Let run this!

To compile I'm assuming you have gcc installed or clang will do fine as well.

~$ gcc thisquestion.c -o thisquestion 

~$ ./thisquestion 

I'm wasted for love, and now I shall show my math skills! 1+0 = 1\nEOF

digits = 1 2 0 0 0 0 0 0 0 0, white space = 15, other = 48

Note: '\n' is when I pressed "ENTER" ( or "RETURN" for MacOS ) and EOF for me ( MacOS is Control-D) and it might be different you. To check enter ~$ stty all in your terminal.

So what does the output say? 

First, the digit '0' repeats ONE time, digit '1' repeats TWICE, there are 14 spaces and ONE '\n' character = 14 + 1 = 15 white spaces, and finally 48 alphabets and special characters (+) included which if you really really want go ahead and count. 

We are done? Yep. We are done.

selected by

Related questions