Thread: Code Critique
View Single Post
Old Jun 2nd, 2006, 3:43 PM   #3
darthsabbath
Newbie
 
Join Date: Dec 2005
Posts: 15
Rep Power: 0 darthsabbath is on a distinguished road
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
darthsabbath is offline   Reply With Quote