-1

C code

#include <stdio.h>

int main () 
{
    int c , nother , new , ndigits [10] , white, tabs ; 

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

    while ( (c = getchar() )!= EOF ) 
    {
        switch (c)
        {
            case '0' : case '1' : case '2' : 
            case '3' : case '4' : case '5' : 
            case '6' : case '7' :  case '8' : 
            case '9' : 
                ndigits[c- '0' ]++ ; 
                break ; 

            case ' '  : 
                printf("w");  /*to see how many spaces */
                white++ ; 

            case '\t' :
                printf("t");
                tabs++;

            case '\n' : 
                printf("n");
                new++ ; 
                break ; 

            default : 
                nother++ ; 
                break ;     
        }
    }

    printf ("digits = " ) ; 

    for ( int i = 0 ; i < 10 ; i++ ) 
        printf ("%d" , ndigits[i]) ;

    printf ( ",tabs = %d , new line = %d,  spaces = %d , other = %d ",
        tabs, white , new , nother ); 

    return 0 ;
}
  1. When I compile it using GCC and just press Ctrl + z it prints

    digits = 00000 , tabs = 4200912 , new line = 4194432 , spaces = 2293540 other = 2147307520

    where these number come from ?

  2. I compile it again and enter HELLO HELLO HELLO and click enter and it prints wtnwtnwnn

    • why is that (there is 3 n than expected , why it counts three tabs ) ?
Paolo
  • 15,233
  • 27
  • 70
  • 91

3 Answers3

2

Initialize to zero the counters otherwise their initial value will be unpredictable.

int c , nother = 0 , new = 0 , ndigits [10] , white = 0, tabs = 0 ;

Also each case block (except the ones that catch digits) must be terminated with break; to achieve the expected result.

If you omit them the next instructions will be executed.

white++;
break;

...

tabs++;
break;

Bottom note: you would have found by yourself these mistakes by just enabling warnings on the compiler. Do it: you'll save a lot of time spotting naive errors.

Paolo
  • 15,233
  • 27
  • 70
  • 91
2

Some of your cases are missing breaks, which doesn't seem to be what you want.

Also you haven't initialized nother, new, white, and tabs, but are using them anyway. This results in undefined behavior. (Any decent compiler will give you a warning about this.)

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
1

There were several mistakes :

  1. you were just declaring the variables and not initializing them.Thus a garbage value was inserted into those variables
  2. It was printing 'n' errorneously as you haven't specified the break for the above statement.
  3. You were printing no. of spaces as if number of lines ( check it printf (",tabs = %d , new line = %d, spaces = %d , other = %d " ,tabs ,white , new, nother) ; )

the following code generate the output you need :

    #include <stdio.h>

     int main () 
       {
    int c=0,nother=0,new=0,ndigits[10],white=0,tabs=0,i=0 ; 
    for ( i = 0 ; i< 10 ; i++)
        ndigits[i] = 0 ;
    while ( (c = getchar() )!= EOF ) 
    {
        switch (c)
        {
            case '0' :
            case '1' :
            case '2' : 
            case '3' :
            case '4' :
            case '5' : 
            case '6' :
            case '7' :
            case '8' : 
            case '9' : 
                      ndigits[c- '0' ]++ ; 
                      break ; 
            case ' '  : 
                       printf("w");  /*to see how many spaces */
                       white++ ; 
            case '\t' :
                       printf("t");
                       tabs++;
                       break;
            case '\n' : 
                           {
                        printf("n");
                        new++ ; 
                        break ; 
                             }
            default : 
                      nother++ ; 
                      break ;     
        }
    }
    printf ("digits = " ) ; 
    for (i = 0 ; i < 10 ; i++ ) 
        printf ("%d" , ndigits[i]) ;
    printf (",tabs = %d , new line = %d,  spaces = %d , other = %d " ,tabs , new,white , nother) ; 
    return 0 ;
      } 
Mathews Sunny
  • 1,796
  • 7
  • 21
  • 31