Change every four letter word to 'love"

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  #include <iostream>
#include <string>
#include <cctype>

int main()
{
    using namespace std;

    string one_line, sentence="";
    cout <<"Enter a sentence."<<endl;
    do
    {
        getline(cin, one_line);
        if (sentence.length()==0)
           sentence=one_line;
        else
           sentence=sentence +" "+ one_line;
    }while (one_line.find(".")==string::npos);
    cout <<"Your sentence corrected for capitalization is"<<endl;
    cout <<sentence <<endl;
    char current;
    bool found_whitespace=false;
    for (int i=0; i <sentence.length();i++)
    {
        current=sentence[i];
        if (i==0)
           current=toupper(current);
        else
           current=tolower(current);
        if(isspace(current))
        {
            if (found_whitespace==false)
            {
                cout << current;
                found_whitespace=true;
            }
            else
            {
                //do nothing because we have repeat whitespace
            }
        }
        else //not whitespace
        {
            cout <<current;
            found_whitespace=false;
        }
    }
    cout <<endl;
    return 0;
}
Even though you have written "Write your question here." and used code tags (I'd actually like to congratulate you, so few new users use them), I'm afraid that 51 lines of code is not a question. Neither is the title, though it is an imperative and therefore commands us to help you, and nowhere do you provide any errors which anyone could fix.
The program should be able to change every four letter word in the sentence typed in by the user to the word "love". The user types in a sentence. After the program has changed the sentence to lower and upper case and taken out the spaces, it must check for words which are four characters in length and change the word to "love"
I still cannot see a question sorry. Are you looking for someone to do the whole thing for you (bad) or do you have a specific question about the task (good)?
http://www.cplusplus.com/reference/string/string/replace/
Last edited on
I need someone to show me how to find every four letter word in the sentence and change it to the word "love'. Every four letter word in the sentence. i managed to do the first part of the problem that is, to change the first letter of the sentence to upper and to take out space and change any capitals to lower in the sentence. its only the last part I am having a problem with to change the four letter word.
But this give me the size of the whole string (sentence). I want the size of a word in the sentence
Then instead of removing all the spaces to start with, use them to find the amount of times you have to increment your position along the string until you find another one. If you find a whitespace, then increment 5 times and find the next whitespace, then you have found a 4 letter word and you change it to "love". Then when you've been through the whole string, you remove the spaces.
Not helpful
Work it out first with pen and paper then translate the process into C++ code.
Leaving out the capitalization part here is some code which replaces each four letter word with the word LOVE.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string sentence;
    int charCount,charPos;
    char letter;
    sentence = "this is a test of the program but will it work";
    charCount = 0;

    cout << sentence << endl;

    for (charPos=0; charPos<sentence.size();charPos++)
        {
        letter = sentence[charPos];
        charCount = charCount + 1;   // count the letters found
        if (letter == 32)               // found a space
        {
            if (charCount == 5)    // count includes the space after the number of letters
            {
                sentence[charPos-4]='L';  //  replace the last four positions with the word LOVE
                sentence[charPos-3]='O';
                sentence[charPos-2]='V';
                sentence[charPos-1]='E';
            }
            charCount=0;   // reset length of words
        }
    }
    if (charCount == 4)    // deal with any last word
        {
            sentence[charPos-4]='L';  //  replace the last four positions with the word LOVE
            sentence[charPos-3]='O';
            sentence[charPos-2]='V';
            sentence[charPos-1]='E';
        }

    cout <<  sentence << endl;
    return 0;
}
Last edited on
It only changes the first letter of the word
closed account (48T7M4Gy)
You need to use the string tokenizer which is a function that splits up the line to produce pointers to tokens (words) in the sentence. Then use string length functionality to check each word length and make the necessary change(s).

http://www.cplusplus.com/reference/cstring/strtok/

Given a position in a string, create a function the finds the start of the next word.

Given the position of the start of a word, create a function that finds the end of the word.

Now you can just call these two functions to find the start and end of each word. If the length of the word is 4, replace it with "love."
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::string;


// Given a stirng and the starting position of a word, return
// the postiion just past the end of the word.  For example,
// if str is "Come in Tokyo" and pos is 5 (the start of "in") then
// this returns 7 (the position just past "in")
size_t
endOfWord(const string & str, size_t pos)
{
    while (pos < str.size() && isalpha(str[pos])) {
        ++pos;
    }
    return pos;
}


// Given a string and a starting position, return the position of the
// start of the next word. In other words, skip non-letters until you
// find one.  Returns str.size() if there are no more words
size_t
startOfWord(const string & str, size_t pos)
{
    while (pos < str.size() && !isalpha(str[pos])) {
        ++pos;
    }
    return pos;
}

int
main()
{
    size_t start, end;
    string str;

    getline(cin, str);

    for (start = startOfWord(str, 0);
         start < str.size(); start = startOfWord(str, end)) {

        end = endOfWord(str, start);    // find end of current word
        if (end - start == 4) {
            // replace 4 letter word with love
            str.replace(start, 4, "love");
        }
    }
    cout << str << '\n';
}


fonz wrote: It only changes the first letter of the word.


Strange it worked fine for me. There were some errors in dealing with any four letter word at the start and end of the sentence which I have since corrected in the previous post.

You could use strtok or the str.replace but rolling your own means you know how it works.

You just need to break the sentence up into words and deal with them accordingly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <cctype>
using namespace std;

int main()
{


    std::cout << "Please enter a sentence ";
    std::string input;
    std::getline(std::cin, input);


    std::string word;
    std::stringstream ss(input);
    int count = 0;
    while(ss >> word)
    {
        //Make the entire word lowercase
        std::transform(word.begin(), word.end(), word.begin(), ::tolower);

        //change the word to love when a 4 letter word is found
        if(word.size() == 4)
            word = "love";


        //Make the first word in the sentence uppercase
        if(count == 0)
            word.front() = toupper(word.front());


        //Print the word
        std::cout << word << ' ';

        ++count;
    }

    return 0;
}



Note: This code does not account for things that are not words such as numbers
Thank you very much guyz.
Topic archived. No new replies allowed.