Solved

coding question?

Posted on 2006-10-19
9
223 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Statment to match two tables in c# 6 77
stack 22 159
Compile VxWorks Toronado project under Visual Studio 11 181
xamarin c# deserialize Json containing nested object 2 81
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

895 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

11 Experts available now in Live!

Get 1:1 Help Now