B
Buck Rogers
Hi guys! Love your work!
The below program is from K&R2, p22.
=================================
#include <stdio.h>
/* count digits, white space, others */
main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c - '0']; <---------------------- /* where I am
confused */
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
printf("digits =");
for ( i = 0; i < 10; ++i)
printf(" %d", ndigit); <--------------------- /* where I am
confused */
printf(", white space = %d, other = %d\n",
nwhite, nother);
return 0;
}
========================================
I can't get my head around what "++ndigit[c - '0']"
means. I understand what c - '0' is for, but ++indigit[]?
That's not incrementing through elements in an array ie. indigit[i++]
(where is is a declared integer)??
If not, what does it mean? Is it incrementing an actual array(not
elements of the array), which I can't figure out.
I just can't figure out how the program can keep a tab on what
digit(s) were entered and then actually prints the number of times
each digit was entered, spaced correctly along the "index".
Can someone please explain the exact code which achieves this to me?
I think this program does alot for such little code!
Thanks in advance!
Buck.
The below program is from K&R2, p22.
=================================
#include <stdio.h>
/* count digits, white space, others */
main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c - '0']; <---------------------- /* where I am
confused */
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
printf("digits =");
for ( i = 0; i < 10; ++i)
printf(" %d", ndigit); <--------------------- /* where I am
confused */
printf(", white space = %d, other = %d\n",
nwhite, nother);
return 0;
}
========================================
I can't get my head around what "++ndigit[c - '0']"
means. I understand what c - '0' is for, but ++indigit[]?
That's not incrementing through elements in an array ie. indigit[i++]
(where is is a declared integer)??
If not, what does it mean? Is it incrementing an actual array(not
elements of the array), which I can't figure out.
I just can't figure out how the program can keep a tab on what
digit(s) were entered and then actually prints the number of times
each digit was entered, spaced correctly along the "index".
Can someone please explain the exact code which achieves this to me?
I think this program does alot for such little code!
Thanks in advance!
Buck.