25: // function to parse words from a string.
26: bool GetWord(char* theString, char* word, int& wordOffset)
27: {
28: if (theString[wordOffset] == 0) // end of string?
29: return false;
30:
31: char *p1, *p2;
32: p1 = p2 = theString+wordOffset; // point to the next word
33:
34: // eat leading spaces
35: for (int i = 0; i<(int)strlen(p1) && !isalnum(p1[0]); i++)
36: p1++;
37:
38: // see if you have a word
39: if (!isalnum(p1[0]))
40: return false;
41:
42: // p1 now points to start of next word
43: // point p2 there as well
44: p2 = p1;
45:
46: // march p2 to end of word
47: while (isalnum(p2[0]))
48: p2++;
49:
50: // p2 is now at end of word
51: // p1 is at beginning of word
52: // length of word is the difference
53: int len = int (p2 - p1);
54:
55: // copy the word into the buffer
56: strncpy (word,p1,len);
57:
58: // null terminate it
59: word[len]=’\0’;
60:
61: // now find the beginning of the next word
62: for (int j = int(p2-theString); j<(int)strlen(theString)
63: && !isalnum(p2[0]); j++)
64: {
65: p2++;
66: }
67:
68: wordOffset = int(p2-theString);
69:
70: return true;
71: }
424 Day 13
LISTING13.7 continued