Having an issue with a project. I have to convert four letter words to love. THe issue is my program will let you enter in a sentence, however it has no output. I think it may have something to do with my while look because I am lost as to what is suppose to go there. At first I had it set to look for whitespace using 'isspace', but received an error. So I tried setting it so the the string for a the fourWord is not whitespace and I end up with no output. HEre is the code:
If undestood it correctly you need to go through each word in the sentence and if that word is 4 characters long replace it with the word "Love", then you need to do something like this
getline(cin,sentence);
//we need to extract each word from the sentence and
// check for its size, if it is a four letter word
// then replace it with "Love"
// ex: "I hate you cpp haha haha 1234
//1. if some empty spces in the begining ignore it and
// find the begining of sentence
string::size_type pos1= sentence.find_first_not_of(" ",0);
//2. find the first space from there
string::size_type pos2=sentence.find_first_of(" ",pos1);
// between these two pos you will get a word
// 3. check for the size and replce it
while(pos1 != string::npos || pos2 != string::npos)
{
fourWord= sentence.substr(pos1,pos2-pos1);
cout << fourWord <<endl;
string::size_type s=fourWord.size();
if(s==4)
sentence.replace(pos1,pos2-pos1,"Love");
pos1= sentence.find_first_not_of(" ",pos2); // next word begin
pos2=sentence.find_first_of(" ",pos1); // next word end
}
cout << sentence << endl;
Of course there is no output. The user types in his/her/it? sentence and it automatically checks the string fourWord without adding anything into fourWord. I don't know exactly how it would be done, but you would need to check the string sentence for four letter words to begin with before even moving on past line 18.