#include "strutils.h" #include #include #include #include void ToLower(char *s) // postcondition: s all lower case { int len = strlen(s); for(int k=0; k < len; k++){ s[k] = tolower(s[k]); } } void ToLower(apstring & s) // postcondition: s all lower case { int len = s.length(); for(int k=0; k < len; k++){ s[k] = tolower(s[k]); } } void ToUpper(apstring & s) // postcondition: s all upper case { int len = s.length(); for(int k=0; k < len; k++){ s[k] = toupper(s[k]); } } void ToUpper(char * s) // postcondition: s all upper case { int len = strlen(s); for(int k=0; k < len; k++){ s[k] = toupper(s[k]); } } void StripPunc(apstring & word) { int first = 0; int len = word.length(); while (first < len && ispunct(word[first])){ first++; } // assert: first indexes either '\0' or non-punctuation // now find last non-nonpunctuation character int last = len - 1; // last char in s while(last >= 0 && ispunct(word[last])){ last--; } word = word.substr(first,last-first+1); } void StripPunc(char *s) { int first = 0; int len = strlen(s); // find first non-punctuation character while (first < len && ispunct(s[first])){ first++; } // assert: first indexes either '\0' or non-punctuation assert(s[first] == '\0' || !ispunct(s[first])); // now find last non-nonpunctuation character int last = len - 1; // last char in s while(last >= 0 && ispunct(s[last])){ last--; } assert(last == -1 || !ispunct(s[last])); s[last+1] = '\0'; // force string to end // shift string left in case there is leading punctuation int k=0; while (s[k] = s[first]){ // loop until '\0' copied k++; first++; } } void StripWhite(apstring & word) { int first = 0; int len = word.length(); while (first < len && isspace(word[first])){ first++; } // assert: first indexes either '\0' or non-punctuation // now find last non-nonpunctuation character int last = len - 1; // last char in s while(last >= 0 && isspace(word[last])){ last--; } word = word.substr(first,last-first+1); } void StripWhite(char * s) // postcondition: s has no leading/trailing white space { int first = 0; int len = strlen(s); // find first non-whitepace character while (first < len && isspace(s[first])){ first++; } // assert: first indexes either '\0' or non-whitespace assert(s[first] == '\0' || !isspace(s[first])); // now find last non-whitespace character int last = len - 1; // last char in s while(last >= 0 && isspace(s[last])){ last--; } assert(last == -1 || !isspace(s[last])); s[last+1] = '\0'; // force string to end // shift string left in case there is leading whitespace int k=0; while (s[k] = s[first]){ // loop until '\0' copied k++; first++; } }