Solved

coding question?

Posted on 2006-10-19
9
212 Views
Last Modified: 2010-04-15
hi, could someone explain to me what is the meaning of " ndigit[c-'0']++" in my following code?
thanks

-----------code--------------------
#include <stdio.h>

 main()  
   {
       int c, i, nwhite, nother, nline,ndigit[10];

       nwhite = nother = 0;
       nline=1;
       for (i = 0; i < 10; i++)
           ndigit[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':
           
               ndigit[c-'0']++;------------------------------------------->??
               break;
           case ' ':
           case '\t':
               nwhite++;
               break;
           case '\n':
               nline++;
               break;
           default:
               nother++;
               break;
           }
       }
       printf("digits =");
       for (i = 0; i < 10; i++)
           printf(" %d", ndigit[i]);
       printf(", white space = %d, line# = %d, other = %d\n", nwhite, nline, nother);
       return 0;
   }
0
Comment
Question by:rmtogether
  • 5
  • 4
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17770575
If the character set used is ASCII, in which the character values '0' ... '9' are contiguous, It counts the number of times c=='0' in ndigit[0], the number of times c=='1' in ndigit[1], ..., the number of times c=='9' in ndigit[9],
0
 

Author Comment

by:rmtogether
ID: 17770624
I still confused about the format [c-'0'], why use c minus zero ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 17770646
Assuming you are using the ASCII character set
'0'-'0'==0
'1'-'0'==1
'2'-'0'==2
0
 

Author Comment

by:rmtogether
ID: 17770801
thanks ozo

I understand it conts the number of times like c=='0' in ndigit[0], why can't I just use c like ndigit[c] without -'0'?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 84

Expert Comment

by:ozo
ID: 17770831
in ASCII, '0'==48 and '9'==57, so you would have to declare int ndigit['9'+1] instead of int ndigit[10]
the for loops would also need to be changed to for (i = '0'; i <= '9'; i++)
0
 

Author Comment

by:rmtogether
ID: 17770890
thans,
So, at begining c is declare as integer, when goes to switch (c), c are converted to character. that is why we need use c-'0'-----> am I correct about this?

does case must use character? like case '0' ? can I use case 0 instead?
0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 17770937
There is no conversion.  getchar() returns an int, and '0' is an integer constant.
If your character set is ASCII, '0'==48
you can use case 0: which is the same as case '\0':
or case 48: which is the same as case case '0': (assuming ASCII)

other ASCII character values
       0 nul    1 soh    2 stx    3 etx    4 eot    5 enq    6 ack    7 bel
       8 bs     9 ht    10 nl    11 vt    12 np    13 cr    14 so    15 si
      16 dle   17 dc1   18 dc2   19 dc3   20 dc4   21 nak   22 syn   23 etb
      24 can   25 em    26 sub   27 esc   28 fs    29 gs    30 rs    31 us
      32 sp    33  !    34  "    35  #    36  $    37  %    38  &    39  '
      40  (    41  )    42  *    43  +    44  ,    45  -    46  .    47  /
      48  0    49  1    50  2    51  3    52  4    53  5    54  6    55  7
      56  8    57  9    58  :    59  ;    60  <    61  =    62  >    63  ?
      64  @    65  A    66  B    67  C    68  D    69  E    70  F    71  G
      72  H    73  I    74  J    75  K    76  L    77  M    78  N    79  O
      80  P    81  Q    82  R    83  S    84  T    85  U    86  V    87  W
      88  X    89  Y    90  Z    91  [    92  \    93  ]    94  ^    95  _
      96  `    97  a    98  b    99  c   100  d   101  e   102  f   103  g
     104  h   105  i   106  j   107  k   108  l   109  m   110  n   111  o
     112  p   113  q   114  r   115  s   116  t   117  u   118  v   119  w
     120  x   121  y   122  z   123  {   124  |   125  }   126  ~   127 del
0
 

Author Comment

by:rmtogether
ID: 17775211
hi, ozo

thank you I got it.
as you said,  getchar() returns an int, and assign to c. So how about
case ' '
case '\t'
case '\n':
0
 
LVL 84

Expert Comment

by:ozo
ID: 17776677
that should be
case ' ':
case '\t':
case '\n':

' ' is sp  space
'\t' is ht  horizontal tab
'\n' is nl  newline
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now