1. ---------------------- Seems to work correctly. 10/10 int compresstokens(char *a){ int i=0,j; /* Remove Adjoining Blanks */ while( a[i] != '\n'){ /* While blank delimiter is adjacent */ while( a[i] == ' ' && a[i+1] == ' '){ j=i; /* Shift left remaining chars */ do{ a[j]=a[j+1]; }while( a[j++] != '\n' ); } i++; } /* Remove Trailing Blank */ if( i > 0 && a[i-1] == ' ' ) a[i-1]=a[i]; /* Remove Leading Blank */ i=0; if( a[i] == ' ' ){ /* If leading char is Blank */ do{ /* Shift left remaining chars */ a[i]=a[i+1]; }while( a[i++] != '\n' ); } if( a[0] == '\n' ) return 0; /* No Tokens */ i=0; /* Count Tokens */ j=1; while( a[i] != '\n' ) if( a[i++] == ' ' ) j++; return j; } 2. ------------------------- Seems to work correctly 10/10 int compresstokens(char *s) { int intoken = 0; /* boolean */ char* lptr; /* lead ptr */ char* tptr; /* trail ptr */ int scount = 0; /* space count */ int tcount = 0; /* token count */ tptr = (lptr = s); /* Remove extra spaces in the front */ while ( *lptr == ' ' ) { lptr++; } while ( *lptr != '\n' ) { if ( *lptr == ' ' ) { scount++; intoken = 0; } else { if (!intoken) { tcount++; } scount = 0; intoken = 1; } if ( scount <= 1 ) { *tptr++ = *lptr; } lptr++; } /* Remove extra spaces at the end */ while ( tcount && *(tptr-1) == ' ' ) { tptr--; } *tptr = '\n'; return tcount; } 3. ------------------------- Seems to work correctly 10/10 int compresstokens(char *s) { char *next = s+1; int numRemoved = 0; //num of spaces removed except for beginning int tokens = 0; /* The algorithm runs in a single loop, thus O(n) time. The loop update is out of the ordinary, it updates the position of the next char in s */ for( s = s; *s != '\n'; next = s+numRemoved+1 ) { //initial space deletion if( tokens == 0 ) { if( *s == ' ' ) { s++; continue; } tokens++; //short circuited, so only executed once } //subsequent space deletion and string updating if( *s == ' ' ) { if( *next == ' ' ) { //grab the next char and increment numRemoved *next = *(next+1); numRemoved++; continue; } if( *next == '\n' ) { *s = *next; continue; } tokens++; s++; *s = *(s+numRemoved); } else { s++; *s = *(s+numRemoved); } } /*Since the array is shifted backwards, and no specifications were given as to what should be done about data past \n, the data has been left without being zeroed out with \0 or such */ return tokens; } 4. ------------------------- Seems to work correctly 10/10 Requires some comments to describe what is happening. int compresstokens(char *s){ int blanks = 0; int move = 0; int tokens = 1; if(*s == ' ') { tokens--; move++; } if(*s == '\n') tokens--; while(*s !='\n') { if ((*s != ' ')&&(*s != '\n')) { s++; *(s-move) = *s; continue; } if((*s == ' ') && (*(s+1) == '\n')){ *(s-move) = *s; tokens--; } if((*s == ' ')&&(blanks == 0)) { tokens ++; if(*(s+1) == ' ') { blanks++; s++; } else if(*(s+1) != ' ') { s++; *(s-move) = *(s); continue; } } if((*s == ' ') && (blanks != 0)) { s++; blanks++; } if(*s != ' ') { move += (blanks-1); *(s-move) = *(s); blanks = 0; } } return tokens; } 5. ------------------------- Tabs should not be treated as blanks. 7/10 Otherwise, seems to be working correctly. int compresstokens(char *s) { int i, j, tokens, character; i=0, j=0, tokens=0, character=0; while(s[i] != '\n') { while((s[i] == ' ') || (s[i] == '\t')){ i++; } while((s[i] != ' ') && (s[i] != '\t') && (s[i] != '\n')) { s[j] = s[i]; i++; j++; character = 1; } while((s[i] == ' ') || (s[i] == '\t')){ i++; } if(s[i] == '\n'){ s[j] = s[i]; if (character == 1) tokens++; } else{ s[j] = ' '; j++; tokens++; } } printf("The array is:%s\n", s); return tokens; } 7. ------------------------- Reports the wrong number of tokens in most cases. Off by one. 5/10 Comments go past column 80 int compresstokens(char *s){ int numtok = 0; int first = 0; char *news; int i; news = s; while(*news!='\n'){ if(first==0 && *news==' '){ //first in array is return s++; if(*(news+1)!=' ') numtok++; } else{ if(*news==' '){ if(*(news+1)==' '){ //check for consecutive blanks i = 0; while(*(news+i)!='\n'){ //shift all chars to left to replace blanks *(news+i) = *(news+i+1); i++; } news--; } else numtok++; //increase token count after non consecutive blank } first = 1; } news++; } return numtok; } 8. ------------------------- Has a side effect of setting errno. 10/10 Seems to work correctly. #include #include int compresstokens(char *s) { int i = 0; int j = 0; int numtokens = 0; int reachtok = 0; char *snew; if (s == NULL) { errno = EINVAL; return -1; } snew = s; /* check for \n and blanks; store the new (possibly shorter) array in array snew; count the number of tokens */ while (s[i] != '\n') { if (s[i] != ' ') { snew[j] = s[i]; i++; j++; reachtok = 1; }//end if else { i++; if (reachtok == 1) numtokens++; reachtok = 0; }//end else }//end while if (reachtok == 1) numtokens++; /* insert \n at end of new array snew */ snew[j] = '\n'; return numtokens; }//end of compresstokens() 9. ------------------------- Seems to work correcly. 10/10 int compresstokens(char *s) { int count = 0; int lead = 0; int trail = 0; while ( s[lead] != '\n' ) { while ( s[lead] == ' ' ) lead++; if ( s[lead] == '\n' ) { s[trail] = '\n'; return count; } while ( s[lead] != '\n' && s[lead] != ' ' ) { s[trail++] = s[lead++]; } count++; if ( s[lead] == '\n' ) { s[trail] = '\n'; return count; } else { s[trail++] = ' '; lead++; } } return count; } 10. ------------------------ Seems to work correctly. 10/10 int compresstokens(char *s) { char *left = s; char *right = s; int tokens = 0; while( *right != '\n' && *right == ' ' ){ right++; } if( *right != '\n' ){ tokens++; } while( *right != '\n' ){ *left++ = *right++; while( *right == ' ' ){ if( *++right != '\n' && *right != ' ' ){ *left++ = ' '; tokens++; } } } *left = *right; return tokens; } 11. ------------------------ Seems to work correctly. 10/10 int compresstokens(char *s){ int count=0; char* lptr; char* tptr; if (s==NULL) return -1; lptr=s; tptr=s; while(*lptr!='\n'){ if(count>0) *tptr++=' '; while(*lptr==' ') lptr++; if(*lptr!=' '&&*lptr!='\n') count++; while(*lptr!=' '&& *lptr!='\n') *tptr++=*lptr++; } *tptr ='\n'; return count; } 12. ------------------------ Reports one too many tokens when there are trailing blanks. 5/10 Too much indentation. See programming style. int compresstokens(char *s){ int ccount = 0; while(s != NULL && *(s+(ccount++)) != '\n'); if(ccount > 0){ char tString[ccount], tchar; int tcount = 0, wcount = 0, i = 0, j = 0; while((tchar = *(s+(j++))) != '\n'){ if(tchar == ' '){ if(wcount == 0){ continue; } else{ tString[i++] = tchar; tcount ++; wcount = 0; } } else{ tString[i++] = tchar; wcount ++; } } if(tcount == 0 && wcount == 0){ return tcount; } else{ tString[i++] = tchar; tcount ++; for(j = 0; j < i ; j ++) *(s+j) = tString[j]; return tcount; } } } 13. ------------------------ Incorrect token count when there are leading or trailing blanks. 5/10 Need include file for NULL int compresstokens(char *s){ int count = 0; char *ptr, *ptr1, *ptr2; if(s == NULL || *s == '\n') return count; ptr = s; while(*ptr != '\n'){ if(*ptr == ' '){ count++; while(*(ptr + 1) == ' '){ ptr1 = (ptr + 1); ptr2 = (ptr1 + 1); while( *ptr2 != '\n'){ *ptr1++ = *ptr2++; } *ptr1 = '\n'; } } else if(*ptr == '\n') count++; ptr++; } return ++count; } 14. ------------------------ Seems to work correctly. 10/10 Need include file for NULL Avoid using C++ style comments. int compresstokens(char *s){ int count = 0; char *ptr1, *ptr2, *wdptr; if( s == NULL || *s == '\n' ) return count; // remove all the white spaces from the front while( *s == ' ' ){ ptr1 = s; ptr2 = (ptr1 + 1); while( *ptr2 != '\n' ) *ptr1++ = *ptr2++; *ptr1 = '\n'; } // return 0 if all that was in the line is spaces if( *s == '\n' ) return count; // remove all spaces from the rear ptr1 = s; while( *ptr1 != '\n' ) // get to the end ptr1++; ptr2 = ptr1 - 1; while( *ptr2 == ' ' ) *ptr2-- = *ptr1--; // remove spaces from anywhere in between and count wdptr = s; while( *wdptr != '\n' ){ if( *wdptr == ' ' ){ count++; while( *(wdptr + 1) == ' ' ){ ptr1 = (wdptr + 1); ptr2 = (ptr1 + 1); while( *ptr2 != '\n' ) *ptr1++ = *ptr2++; *ptr1 = '\n'; } } wdptr++; } return ++count; } 15. ------------------------ Seems to work correctly 10/10 Avoid C++ style comments Start a new line after if and while. int compresstokens(char *s) { int curPos = 0; // runs entire array length int numTokens = 0; int ndx = 0; // keeps new character positon while( s[curPos] != '\n' ) { while( s[curPos] == ' ' ) { curPos++; } if( s[curPos] == '\n' ) { return numTokens; } else { while( s[curPos] != ' ' ) { s[ndx] = s[curPos]; curPos++; ndx++; if( s[curPos] == '\n' ) { break; } } if( s[curPos] == '\n' ) { s[ndx] = '\n'; numTokens++; return numTokens; } else { s[ndx++] = ' '; numTokens++; } } } //end while '\n' return numTokens; } // end compresstokens() 16. ------------------------ Seems to work correctly. 10/10 Need include file for NULL Comments go past column 80 int compresstokens(char *s) { /* function to count tokens in arry s, term'd by a newline * remove excess ws before after & between tokens, * and return token count */ char * l_ptr = s, * r_ptr = s; int tok_count = 0; if (r_ptr == NULL) return tok_count; /* no tokens */ while (*r_ptr == ' ') r_ptr++; /* move r_ptr only, skipping sw, either '\n', or 1st char of tok */ while (*r_ptr != '\n') { /* r_ptr now points to 1st token char */ tok_count++; /* keeping track of # of tokens */ while ((*r_ptr != '\n') && (*r_ptr != ' ')) { *l_ptr = *r_ptr; /* move thru copying this token w/o ws, in place */ l_ptr++; r_ptr++; } if (*r_ptr == ' ') { while (*r_ptr == ' ') r_ptr++; /* move r_ptr only, skipping sw, not copying */ if (*r_ptr != '\n') { /* if this is a tok, need space */ *l_ptr = ' '; l_ptr++; } } } /* end of token while loop */ *l_ptr = '\n'; /* if r_ptr is at \n, l_ptr should be at \n too */ return tok_count; } 17. ------------------------ Need prototypes for forward references. 9/10 There should always be a statement following an if There are unused variables in some of the functions. There is no need to use an extra array for this problem. int compresstokens (char *s) { int i = 0, j = 0; /* Array Counters */ for (i=0; s[i] != '\n'; i++); /* Count the number of char in s */ removeExtraBlanks (s, i); if (s[0] == '\n'){ return 0; } return countTokens(s); } /* End of compresstokens Function */ void removeExtraBlanks (char *s, int sLength) { int i = 0, j = 0; /* Array Counters */ char tempS[sLength]; /* Temp String */ /* remove initial blanks */ for (i = 0; s[i] == ' '; i++); /* remove extra internal blanks */ for (;s[i] != '\n'; i++) { if ((s[i] == ' ' && s[i+1] == ' ') || (s[i] == ' ' && s[i+1] == '\n')); else { tempS[j] = s[i]; j++; } /* End of else statement */ } /* End of for loop */ tempS[j] = '\n'; tempS[j+1] = '\0'; for (i=0;i0 && s[c-1] == ' ') { s[c-1] = '\n'; c--; } /* strip multiple blanks */ c = 0; while (s[c] != '\n') { if ( s[c] == ' ' && s[c+1] == ' ' ) { k = c; while (s[k] != '\n') { s[k] = s[k+1]; k++; } } else c++; } /* count tokens */ c = 0; toks = 0; in = 0; while (s[c] != '\n') { if ( s[c] != ' ' && in == 0 ) { toks++; in = 1; } if ( s[c] == ' ') in = 0; c++; } return toks; } 22. ------------------------ Seems to work correctly. 10/10 Avoid C++ style comments int compresstokens(char *s) { char * lptr; char * tptr; int numtokens; lptr = s; tptr = s; // find first token while( (*lptr) == ' '){ lptr++; } // traverse array and compact spaces do{ *(tptr) = *(lptr++); tptr++; if( *lptr == ' '){ *tptr = *lptr; tptr++; while( (*lptr) == ' '){ lptr++; } } }while((*lptr) != '\n'); //set end newline char and check if there is an extra blank *tptr = '\n'; tptr--; if (*tptr == ' ') *tptr = '\n'; // now count the tokens i.e. count space between tokens lptr = s; if( *lptr == '\n'){ return 0; } numtokens = 1; while( *lptr != '\n'){ lptr++; if(*lptr == ' ') numtokens++; } return(numtokens); } 23. ------------------------ Reports one too few tokens in most cases. 5/10 int compresstokens(char *s){ int i=0; int numtokens = 0; while(s[i]==' '){ s[i]=s[i+1]; i++; } while(s[i]!='\n'){ if(s[i]==' '){ s[i]=s[i+1]; i++; while(s[i]==' '){ s[i]=s[i+1]; i++; } numtokens++; } else i++; } return numtokens; } 24. ------------------------ Fails when there are trailing blanks. 5/10 int compresstokens(char *s){ int count; char *curr, *next; count = 0; curr = s; next = s; next++; if (*curr == '\n') return count; /* gets rid of leading spaces and return zero if there are no characters */ if(*curr == ' '){ while(*next== ' ' || *next== '\n') next++; *curr=*next; if(*curr=='\n') return count; curr++; next++; } count++; while (*next!='\n'){ if(*next==' '){ /*if while loop gets rid of excess blanks */ curr++; *curr=*next; next++; while(*next==' ') next++; if(*next!='\n') count++; } curr++; *curr=*next; next++; } curr++; *curr=*next; return count; } 25. ------------------------ Seems to work correctly. 10/10 Consider using array notations, s[size] instead od *(size + size) Do not use lines longer than 80 characters. int compresstokens(char *s){ int i; int j; int k; int size; int tokencount; /*****************************************************************************/ /* Function takes an array of characters ending in a newlince char., Deletes */ /* all preceding and trailing blanks. Also deletes all blank chars in a row */ /* down to 1 blank. Then the function counts the number of words *tokens* in */ /* the arraty and returns this value. Only returning a 0 if there is only a */ /* newline char in the array and no other chars. */ /*****************************************************************************/ i = 0; size = 0; /* Find the size of the array of characters to make sure no out of bounds */ while (*(s + size )!= '\n') size++; while ( i < size && *(s + i) != '\n'){ if (*(s + i) == ' '){ j = i + 1; /* Places j at the end of the blanks. */ while (*(s + j) == ' ' && j < size ){ j++; } /* First if takes care of preceding blanks. */ /* First else takes care of the standard case. */ /* Last else takes care of ending blanks. */ if (i == 0) k = 0; else if (*(s + j) != '\n') k = i + 1; else { *(s + i) = '\n'; for (k = i + 1; k <= j; k++) *(s + k) = '\0'; k = j; i = size; } /* Moves all chars down to replace blanks. */ if (j != k){ while (*(s + j) != '\n' && j < size){ *(s + k) = *(s + j); *(s + j) = '\0'; j++; k++; } *(s + k) = *(s + j); *(s + j) = '\0'; } } i++; } /* Counts the number of tokens and returns the count. */ if ( *(s + 0) == '\n') tokencount = 0; else tokencount = 1; i = 0; while ( *(s + i) != '\n' && i < size){ if ( *(s + i) == ' ') tokencount++; i++; } return tokencount; } 26. ------------------------ Fails when there are leading blanks. 5/10 int compresstokens(char *s){ int count = 0; char *ptr, *lptr, *tptr; if( s == NULL || *s == '\n') /*check for NULL or zero length*/ return count; lptr =s; while( *lptr == ' ' ){ /*find spaces in front*/ tptr = (lptr + 1); while( *tptr != '\n' ){ /*delete spaces in front*/ *lptr=*tptr; lptr++; tptr++; } *lptr = '\n'; } if( *s == '\n' ) return count; lptr = s; while( *lptr!= '\n' ) /*goto end*/ lptr++; tptr = lptr - 1; while( *tptr == ' ' ){ /*delete spaces from end*/ *tptr = *lptr; lptr--; tptr--; } ptr = s; while( *ptr != '\n' ){ if( *ptr == ' ' ){ count++; while( *(ptr + 1) == ' ' ){ /*if double space*/ lptr = (ptr + 1); tptr = (lptr + 1); while( *tptr != '\n' ){ /*delete*/ *lptr = *tptr; lptr++; tptr++; } *lptr = '\n'; } } ptr++; } return ++count; } 27. ------------------------ Seems to work correctly 10/10 The logic could be improved to limit the number of levels of indentation int compresstokens(char *s) { /* precondition: char *s has been initialized and is terminated by a newline postcondition: leading/trailing/consecutive blanks are removed from char *s number of tokens found is returned */ int i = 0; int tokens = 0; while(s[i] != '\n') { if(s[i] == ' ') { /* char is a blank */ if(s[i+1] == ' ' || s[i+1] == '\n' || i == 0) { int c = i+1; while(s[c-1] != '\n') { /* shift left */ s[c-1] = s[c]; c++; } i--; /* decrement to stay at current index */ } } else { /* char is non-blank */ if(i == 0) { tokens++; } else if(s[i-1] == ' ') { tokens++; } } i++; } return tokens; } 28. ------------------------ Modifies memory past the end of the array. 5/10 Does not always compress correctly. Comments are hard to read. Comment lines are too long. int compresstokens(char *s) { int i,j,k,num,size; i = 0; j = 0; k=-1000; num=0; /*----C-O-U-N-T--O-R-I-G-I-N-A-L--S-I-Z-E--O-F--S-T-R-I-N-G-----*/ while( s[i]!='\n') { i++; } size=i; i=0; /*-------E-L-I-M-I-N-A-T-E--W-H-I-T-E--S-P-A-C-E--A-T--T-H-E--E-N-D------*/ while ( s[size-1-i] == ' ') { s[size-1-i]=s[size-i]; i++; } s[size-i+1]='\0'; i=0; /*-----C-O-U-N-T--T-H-E--N-U-M-B-E-R--O-F--T-O-K-E-N-S--------*/ while (s[i]!='\n') { if( s[i]==' ') { j=i; if ((j-k)!=1) { num+=1; } k=j; } i++; } i=0; j=0; k=0; /*-------E-L-I-M-I-N-A-T-E--W-H-I-T-E--S-P-A-C-E--A-T--T-H-E--B-E-G-I-N-N-I-N-G------*/ while( s[i]==' ') { i++; } while( s[j] != '\n') { if ( (j+i)<=size ) s[j]=s[j+i]; else break; j++; } i=0; /*------F-I-N-D--N-E-W--S-I-Z-E---------*/ while( s[i] != '\n'){ i++; } size=i;i=0; /*-------E-L-I-M-I-N-A-T-E--W-H-I-T-E--S-P-A-C-E--I-N--T-H-E--M-I-D-D-L-E-------*/ while(s[i] != '\0') { if(s[i]==' ') { k=i;j=i; while(s[j]==' ') { j++; } k++; while (s[j] != '\n') { s[k]=s[j]; k++; j++; } s[k]='\n'; s[k+1]='\0'; } i++; } return num; } 29. ------------------------ invalid placement of declaration of temp2 5/10 sometimes reports wrong number of tokens. int compresstokens(char *s) { int tokens = 0; char *copy = s; while(*copy != '\n') { if(*copy == ' ' && *(copy + 1) == ' ') { char *temp1 = copy; while(*temp1 == ' ') { temp1++; } char *temp2 = copy + 1; do { *temp2 = *temp1; temp1++; temp2++; } while(*temp1 != '\n'); *temp2 = *temp1; tokens++; } copy++; } return tokens; } 30. ------------------------ Modifies memory outside of array if input contains only newline 5/10 int compresstokens(char *s) { int i, j; /* source/destination indices */ int tokens = 0; int ws = 1; /* Boolean: encountered whitespace? */ i=j=0; while(s[i] != '\n') { if(!ws) { s[j++] = s[i]; if(s[i] == ' ') ws = 1; } else if(s[i] != ' ' && ws) { s[j++] = s[i]; ws = 0; tokens++; } i++; } if(j && s[j-1] != ' ') s[j] = '\n'; else s[j-1] = '\n'; return tokens; } 31. ------------------------ s is not a string. 0/10 You cannot use the string functions for this problem. int compresstokens (char *s){ char *token; char *traverse; int num = 0; if (s == NULL) return num; /* remove beginning blanks */ if ((*s == ' ')||(*s == '\t')){ s++; while ((*s == ' ')|| (*s == '\t')) s++; } traverse = strtok(s," \t"); if(traverse!=NULL) for(num=1;((token=strtok(NULL," \t"))!=NULL)&&(*token!='\n'); num++){ strcat(traverse," "); strcat(traverse,token); } strcat(traverse,"\n"); return num; } 32. ------------------------ This does not do any compression. 0/10 int compresstokens(char *s) { int i; int count = 0; for(i = 0; s[i]; i++) { count = count + 1; } return count; } 33. ------------------------ This only initializes i, not count, k, or j. 0/10 First while may be infinite loop. int compresstokens(char *s){ int count, k, j, i = 0; while(s[k] == ' ' && s[k] != '\n'){ s[k] = s[k+1]; } while(s[j] != '\n'){ j++; } while(s[j-1] == ' ' && j > 0){ s[j-1] = s[j]; } while(s[k] != '\n'){ if(s[k] == ' '){ if(s[k+1] == ' '){ while(s[k+1] == ' '){ s[k] = s[k+1]; } } } k++; } while(s[i] != '\n'){ if(s[i] = ' ' ) count++; } count++; /* for token befor the newline */ return count; } 34. ------------------------ Please only indent 3 spaces. 10/10 Use the style given on the web page. Use ptr to represent a pointer, not an index. Seems to work OK. int compresstokens(char *s) { int ptr1 = 0; int numTok = 0; int ptr2 = 0; while( s[ptr1] != '\n' ) { while( s[ptr1]==' ') { ptr1++; } if( s[ptr1] == '\n' ) { return numTok; } else { while(s[ptr1]!=' ') { s[ptr2] = s[ptr1]; ptr1++; ptr2++; if( s[ptr1] == '\n' ) { break; } } if( s[ptr1] == '\n' ) { s[ptr2] = '\n'; numTok++; return numTok; } else { s[ptr2++] = ' '; numTok++; } } } return numTok; } 35. ------------------------ Reports an incorrect number of tokens. 5/10 Consider using array notation, s[i] instead of *(s+i) int compresstokens(char *s) { int i = 0; int j = 0; int tokens = 0; char *t; t=s; /* temp pointer to beg of s */ while (*(s+i)!='\n'){ while(*(s+i)=='\b') i++; /* skip any blanks */ while (*(s+i)!='\b'&&*(s+i)!='\n'){ *(t+j)=*(s+i); /* overwrite begining of buffer s */ j++; i++; } /* end while copy token */ ++tokens; /* count tokens */ if(*(s+i)=='\b'){ *(t+j)=*(s+i); j++; } /* end if */ if(*(s+i)=='\n') *(t+j)=*(s+i); /* end if */ } /* end while */ return tokens; } 36. ------------------------ Removes all blanks. 5/10 incorrect use of NULL Should not treat tabs as special Falls off bottom without returning a value. int compresstokens(char *s){ int count = 0; char *p = s; if(*p == '\n'){ return count; } while(*p != '\n'){ if(*p=='\r'||*p=='\t'||*p=='\b'||*p==' '){ while(*p=='\r'||*p=='\t'||*p=='\b'||*p==' '){ if (*p == '\n'){ *++s = *p; *s = NULL; *p = NULL; return count; } p++; } count++; } if (*p == '\n'){ *++s = *p; *s = NULL; *p = NULL; return count; } *s++ = *p++; } } 37. ------------------------ Incorrect use of free 0/10 Produces a core dump. int compresstokens(char *s){ char *temp; int token=0; int j=0; int i=0; while((s[i]==' ')||(s[i]=='\t')) i++; while(s[i]!='\n'){ if((s[i]!=' ')&&(s[i]!='\t')){ temp[j]=s[i]; j++; if(((s[i+1]==' ')||(s[i+1]=='\t'))||(s[i+1]=='\n')) token++; } else if((s[i]==' ')||(s[i]=='\t')){ if((s[i+1]==' ')||(s[i+1]=='\t')) i++; } i++; } temp[j]='\n'; free(s); j=0; while(temp[j]!='\n'){ s[j]=temp[j]; j++; } s[j]=temp[j]; return token; } 38. ------------------------ Needs include file to use NULL 5/10 No more than one assignment on a line. tokens never modified. Reports 0 tokens. int compresstokens(char *s){ int current, front, last, tokens; tokens = last = current = front =0; if(s == NULL || s[0] == '\n') return 0; /* find position of /n */ while (s[last] != '\n') last++; /* remove rear blanks */ current = last-1; while (current >=0 && s[current] == ' '){ printf("%c \n",s[current]); current--; } if(current !=last -1){ last = current +1; s[last] = '\n'; } if(last == 0){ return 0; } /* remove front blanks */ current = 0; while(s[current] == ' '){ current++; } if (current != 0){ front = 0; while(current <= last){ s[front] = s[current]; front++; current++; } } return tokens; } 39. ------------------------ isspace does not only check for a blank. 0/10 temp is not initialized. dumps core. int compresstokens(char *s){ int i; int j = 0; int on = 1; int count = 0; char *temp; for(i = 0; s[i] != '\n'; i++){ if(isspace(s[i])) on = 1; else if(on){ on = 0; count++; temp[j] = s[i]; j++; } } return count; } 40. ------------------------ Dumps core on first test. 0/10 Carriage return and tab are not special. #include #include int compresstokens (char *s){ int numtoken = 0, i = 0; char *temp_s = s; while (*temp_s != '\n' || *temp_s != '\r'){ if (*temp_s == ' ' || *temp_s == '\t'){ while(*temp_s == ' ' || *temp_s == '\t'){ temp_s++; } } else if(*temp_s != ' '||*temp_s != '\t'||*temp_s != '\n'||*temp_s != '\r'){ numtoken++; while(*temp_s != ' '||*temp_s != '\t'||*temp_s != '\n'||*temp_s != '\r'){ *(s+i) = *temp_s; temp_s++; i++; } *(s+i) = ' '; i++; } } if(*temp_s == '\n' || *temp_s == '\r'){ if(i > 0){ i--; *(s+i) = '\n'; } else{*s = '\n';} } return numtoken; } 41. ------------------------ Need include file to use NULL 10/10 Seems to work correctly. int compresstokens(char *s){ char currentChar; int currentPosition = 0; int spacesToRemove = 0; int tokensFound = 0; if (s == NULL) { return 0; } else { currentChar = s[currentPosition]; while (currentChar == ' ') { currentChar = s[++currentPosition]; spacesToRemove++; } while (currentChar != '\n') { tokensFound++; while (currentChar != ' ' && currentChar != '\n') { s[currentPosition - spacesToRemove] = currentChar; currentChar = s[++currentPosition]; } if (currentChar == ' ') { s[currentPosition - spacesToRemove] = ' '; spacesToRemove--; } while (currentChar == ' ') { spacesToRemove++; currentChar = s[++currentPosition]; } } if (s[currentPosition -1] == ' ') s[currentPosition - spacesToRemove - 1] = '\n'; else s[currentPosition - spacesToRemove] = '\n'; } return tokensFound; } 42. ------------------------ Gives segmentation fault on first test. 0/10 Function falls off end without returning a value int compresstokens(char *s) { int chomp=0, numtokens=0, idx=0; while ( 1 ) { while ( s[idx] == ' ' ) { for(chomp = idx; s[chomp] != '\n'; chomp++) s[chomp] = s[chomp+1]; } if ( idx > 0 ) { if ( s[idx-1] == ' ' && s[idx] == '\n' ) s[idx-1] = '\n'; } if ( s[idx] == '\n' ) return numtokens; while( s[idx] != ' ' && s[idx] != '\n') idx++; numtokens++; idx++; } } 43. ------------------------ Seems to run OK 10/10 /* tokens.c this program counts and returns the number of tokens in the array of characters terminated by '\n', and removes extra white spaces between the tokens. */ #include #define IN 1 #define OUT 0 int compresstokens(char *s){ char * lptr, *tptr; int numTokens, state; if(s == NULL) return -1; lptr = s; tptr = s; numTokens = 0; state = OUT; while(*lptr != '\n'){ if(*lptr == ' ' && state == IN){ if(numTokens > 0) *tptr++ = ' '; state = OUT; } else if(*lptr != ' '){ if(state == OUT){ state = IN; numTokens++; } *tptr++ = *lptr; } lptr++; } *tptr = '\n'; return numTokens; } ------------------------------------------------------------------