#include #include #include "Sentence.h" #include Sentence::Sentence() { // default constructor sent.resize(0); sent = ""; } Sentence::Sentence(string & source) { // construct using a string int len = source.length(); // find source string length // remove any line feeds from the string int posFound = source.find("\n"); while ((posFound >= 0) && (posFound < len)) { source.replace(posFound,1,""); // replace char found with nothing posFound = source.find("\n"); // see if there is another of that char int len = source.length(); // update length since it has been reduced } sent.resize(len); // make data member be the correct length sent = source; // fill with source string sent[0] = tolower(sent[0]); // force first char to be lower case to help searching } Sentence::Sentence(istream & strm) { // construct using input from a stream // assume sentences end in . cout << "Please enter a sentence ending in a period" << endl << " " << flush; // getline(strm,sent,'.'); string source; getline(strm,source,'.'); int len = source.length(); // find source string length // remove any line feeds from the string int posFound = source.find("\n"); while ((posFound >= 0) && (posFound < len)) { source.replace(posFound,1,""); // replace char found with nothing posFound = source.find("\n"); // see if there is another of that char int len = source.length(); // update length since it has been reduced } sent.resize(len); // make data member be the correct length sent = source; // fill with source string } // inspectors int Sentence::HowManyWords() { // how many words in the sentence int num = CountBetween (" .,!?:"); // find how many words - setting what can divide words return num; } double Sentence::AveWordLen() { // ave word length in sentence int sum = 0; // start total num chars at 0 string words[100]; // assume no more than 100 words in a sentence int num = split(" .,!?:",words); // pull out words and find out how many there are for (int cnt = 0; cnt < num; cnt++) { sum += words[cnt].length(); // add current word length } double ave = (sum * 1.0) / num; return ave; } int Sentence::LongestWordLen() { // length of longest word in sentence int max = 0; // start max num chars at 0 string words[100]; // assume no more than 100 words in a sentence int num = split(" .,!?:",words); // pull out words and find out how many there are for (int cnt = 0; cnt < num; cnt++) { if (words[cnt].length() > max) { // check current word length max = words[cnt].length(); // update largest } } return max; } string Sentence::LongestWord(){ // report what the longest word is int max = 0; // start max num chars at 0 string longest = ""; // start with empty longest string words[100]; // assume no more than 100 words in a sentence int num = split(" .,!?:",words); // pull out words and find out how many there are for (int cnt = 0; cnt < num; cnt++) { if (words[cnt].length() > max) { // check current word length max = words[cnt].length(); // update largest longest = words[cnt]; // do we need to resize it? or does = take care of that? } } return longest; } // mutators // stick toInsert string immediately before first occurrence of toFind string bool Sentence::InsertBeforeFirst(string & toFind, string & toInsert) { bool ok = false; // default to didn't work int posFound = sent.find(toFind); // find string looking for int sLen = sent.length(); // check first if really was found !!!!! if ((posFound >= 0) && (posFound < sLen)) { int prevPos = posFound - 1; sent.insert(prevPos,toInsert); // insert string after char before found ok = true; } return ok; } // stick toInsert string immediately after first occurrence of toFind string bool Sentence::InsertAfterFirst(string & toFind, string & toInsert) { bool ok = false; // default to didn't work int posFound = sent.find(toFind); // find string looking for int sLen = sent.length(); // check first if really was found !!!!! if ((posFound >= 0) && (posFound < sLen)) { int addPos = posFound + toFind.length(); // add after end of toFind string sent.insert(addPos,toInsert); // insert string after string found ok = true; } return ok; } bool Sentence::InsertBeforeAll(string & toFind, string & toInsert) { // stick toInsert string immediately before all occurrence of toFind string bool ok = false; // default to didn't work int posFound = sent.find(toFind); // find string looking for int sLen = sent.length(); int len = toFind.length(); int iLen = toInsert.length(); // check first if really was found !!!!! while ((posFound >= 0) && (posFound < sLen)) { int prevPos = posFound - 1; sent.insert(prevPos,toInsert); // insert string after char before found ok = true; posFound = sent.find(toFind,posFound+iLen); // see if there is another of the toFind string // start looking after string inserted int sLen = sent.length(); // update length since it has been reduced } return ok; } bool Sentence::InsertAfterAll(string & toFind, string & toInsert) { // stick toInsert string immediately after all occurrence of toFind string bool ok = false; // default to didn't work int posFound = sent.find(toFind); // find string looking for int sLen = sent.length(); int len = toFind.length(); int iLen = toInsert.length(); // check first if really was found !!!!! while ((posFound >= 0) && (posFound < sLen)) { int addPos = posFound + len; // add after end of toFind string sent.insert(addPos,toInsert); // insert string after string found ok = true; posFound = sent.find(toFind,addPos+iLen); // see if there is another of the toFind string // start looking after string inserted int sLen = sent.length(); // update length since it has been reduced } return ok; } // remove all occurences of toFind string bool Sentence::RemoveAll(string & toFind) { bool ok = false; // default to didn't work // keep looping as long as the toFind string keeps being found int posFound = sent.find(toFind); int sLen = sent.length(); int len = toFind.length(); while ((posFound >= 0) && (posFound < sLen)) { sent.replace(posFound,len,""); // replace string found with nothing ok = true; posFound = sent.find(toFind); // see if there is another of that char int sLen = sent.length(); // update length since it has been reduced } return ok; } // remove first occurence of toFind string bool Sentence::RemoveFirst(string & toFind) { bool ok = false; // default to didn't work int posFound = sent.find(toFind); // find string looking for int sLen = sent.length(); int len = toFind.length(); // check first if really was found !!!!! if ((posFound >= 0) && (posFound < sLen)) { sent.replace(posFound,len,""); // replace string found with nothing ok = true; } return ok; } // remove all occurrences of any char in the toFind string bool Sentence::RemoveAllChars (string & toFind) { bool ok = false; // default to didn't work int sLen = sent.length(); // work one char at a time int len = toFind.length(); for (int cnt = 0; cnt < len; cnt++) { // string curr = toFind[cnt]; // pull out char to remove // string curr(toFind[cnt]); // pull out char to remove string curr; curr += toFind[cnt]; // keep looping as long as that char keeps being found int posFound = sent.find(curr); while ((posFound >= 0) && (posFound < sLen)) { sent.replace(posFound,1,""); // replace char found with nothing ok = true; posFound = sent.find(curr); // see if there is another of that char int sLen = sent.length(); // update length since it has been reduced } } // end for loop return ok; } // allows outputting all info about sentence ostream& operator << (ostream & strOut, Sentence & mySent) { // just need to output string strOut << mySent.GetString(); return strOut; } // return how many words were found // make sure caller sends a big enough array int Sentence::CountBetween (const string & separators) { int numWords = 0; int n = sent.length(); // find first non-separator char (pass separators and // position to start looking) int start = sent.find_first_not_of(separators,0); // loop as long as there is still a non-separator char while ((start >= 0) && (start < n)) { // find end of current word (pass separators and // position to start looking) int stop = sent.find_first_of(separators,start); // avoid running past end of text if ((stop < 0) || (stop > n)) { stop = n; } numWords++; // find start of next word start = sent.find_first_not_of(separators,stop+1); // cout << " start: " << start << " "; } return numWords; } // return how many words were found // make sure caller sends a big enough array int Sentence::split (const string & separators, string words[]) { int numWords = 0; int n = sent.length(); // find first non-separator char (pass separators and // position to start looking) int start = sent.find_first_not_of(separators,0); // loop as long as there is still a non-separator char while ((start >= 0) && (start < n)) { // find end of current word (pass separators and // position to start looking) int stop = sent.find_first_of(separators,start); // avoid running past end of text if ((stop < 0) || (stop > n)) { stop = n; } // add word to array of words // substr is passed a position to start and a length words[numWords] = sent.substr(start, stop-start); // cout << "current word: " << words[numWords] << endl; numWords++; // find start of next word start = sent.find_first_not_of(separators,stop+1); // cout << " start: " << start << " "; } return numWords; }