Quote:
|
Originally Posted by grumpy
3) If the first character in your input is a space, then the logic will be (commented)
nc = 0;
while ((c = getchar()) != EOF) {
++nc; /* nc now 1 */
if (c == ' ' || c == '\n' || c == '\t' || c == EOF){ /* we've entered a space, so ... */
--nc; /* nc now zero */
if (nc <= MAXSIZE) { /* this test is true */
arr[nc-1]++; /* arr[-1]++ */ so your code exhibits undefined behaviour at this point.
that value, decrementing highest, and going over and over again).
|
So, using the ctype.h header and the isspace() function, would this be what you're getting at? (Sorry, I'm not at a computer with a compiler, so I can't test this)
while ((c = getchar()) != EOF) {
++nc;
if (isspace(c) ){
--nc;
if ((nc > 0) && (nc < MAXSIZE)) {
++arr[nc-1];
nc = 0;
} else if (nc > MAXSIZE) {
arr[MAXSIZE]++;
nc = 0;
}
}
}
Phil