Our version of getword does not properly handle underscores, string constants, comments, or preprocessor control lines. Write a better version.
This is the exercise 6-1 and can be foud on K&R 2 at page 150. http://net.pku.edu.cn/~course/cs101/2008/resource/The_C_Programming_Language.pdf
My solution:
static int isValidKeyWord(char c) {
if(isalnum(c) || c == '_') {
return 1;
}
return 0;
}
int getword(char *word, int lim) {
int c;
char *w = word;
int ordinaryKeyWord = 0;
int comment = 0;
int stringConstant = 0;
while(isspace((c = getch())))
;
if(c == '#' || c == '_' || isalpha(c)) {
ordinaryKeyWord = 1;
*w++ = c;
}
else if(c == '/') {
*w++ = c;
c = getch();
if(c == '*') {
*w++ = c;
comment = 1;
}
else {
*w = '\0';
return *--w;
}
}
else if(c == '\"') {
*w++ = c;
stringConstant = 1;
}
else {
*w++ = c;
*w = '\0';
return c;
}
for(; --lim; w++) {
*w = getch();
if(ordinaryKeyWord && (!isValidKeyWord(*w))) {
ungetch(*w);
break;
}
else if(stringConstant && *w == '\"') {
w++;
break;
}
else if(comment && *w == '*') {
*++w = getch();
if(*w == '/') {
w++;
break;
}
else {
ungetch(*w);
w--;
}
}
}
*w = '\0';
return word[0];
}
There are 3 main cases:
- case 1: comments, if the first two characters are
/
and*
. In this case the function should return when the corresponding*
and/
are met. - case 2: string constants, if the first character is a
"
, then the function should return when the closing"
is met. - case 3: words that begin with
#
,_
and letters. In this case, the program should return when a character different that_
or an alphanumeric character is met.
First, the program analyzes which case is. Based on that, "it knows" when to stop, then it returns the first character of that word.