/* some_hw1_funcs.c Functions from Anastasio's solution to HW-1 CMSC-202, Fall 1997 Created: 23 September 1997 Current: 23 September 1997 */ /* Note: no string functions were used. Only material covered in CMSC-201 was used. 1) lengthOfWord was written to avoid using strlen 2) getNextTokenPosition and getNextSpacePosition were written to avoid using strtok 3) localMatch can be used to do each of the pattern-matching operations (at end of word, anywhere in word, and at specific location in word). /* lengthOfWord * Returns the length of word. Yes, does the same thing as strlen. * word must be a valid C-string (null-terminated char array). */ static int lengthOfWord(word_type word) { int len = 0; while(word[len] != '\0') len = len + 1; return len; } /* localMatch * Returns TRUE if, starting at word index word_i, each character of * pattern matches the corresponding character in word. Example: * the pattern "abc" does match the word "qrsabcdef" at word index 3. * Returns FALSE otherwise. */ int localMatch(word_type word, word_type pattern, int word_i) { int pat_i; int patlen = lengthOfWord(pattern); int wordlen = lengthOfWord(word); /* if pattern is too long, it can't match */ if ( patlen > (wordlen - word_i)) return FALSE; /* scan pat and word, checking char by char */ for (pat_i = 0; pat_i < patlen; pat_i++) if ( word[pat_i + word_i] != pattern[pat_i] ) return FALSE; /* if you get here, it must have matched */ return TRUE; } /* getNextTokenPosition * Returns the index in entry, starting at startpos, that has a * non-whitespace character. Assumes startpos is within the string * and that null character is not encountered. */ static int getNextTokenPosition(char * entry, int startpos) { while ( isspace(entry[startpos]) ) { startpos++; } return startpos; } /* getNextSpacePosition * Returns the index in entry, starting at startpos, that has a * whitespace character (or null). Assumes startpos is within the string. */ static int getNextSpacePosition(char * entry, int startpos) { while ( !isspace(entry[startpos]) ) { if ( entry[startpos] == '\0' ) return startpos; startpos++; } return startpos; }