URGENT HELP PLEASE!!!!!!!!!! THE FOURTH LETTER AFTER A THREE LETTER DON'T CHANGE.

Write a program that reads in a line of text and replaces all four-letter words
with the word “love”. For example, the input string
I hate you, you dodo!

should produce the output
I love you, you love!
Of course, the output will not always make sense. For example, the input
string
John will run home.
should produce the output
Love love run love.
If the four-letter word starts with a capital letter, it should be replaced by
"Love", not by "love". You need not check capitalization, except for the
first letter of a word. A word is any string consisting of the letters of the
alphabet and delimited at each end by a blank, the end of the line, or any
other character that is not a letter. Your program should repeat this action
until the user says to quit.


#include <iostream>
#include <string>


using namespace std;



int main()

{

string str1, str2;

int word_length = 0;




cout << " Hello! Enter a string:\n""\n";

getline(cin, str1);



str2 = str1;



int strL = str1.length();



for(int i = 0; i < strL; i++)

{

char item = str2[i];

if(item != ' ')

word_length++;

else if(item = ' ')

{

if(word_length == 4)

{

if(isupper(str2[i-4]))

str2[i-4] = 'L';

else

str2[i-4] = 'l';

str2[i-3] = 'o';

str2[i-2] = 'v';

str2[i-1] = 'e';

}
word_length = 0;

}

else if(item = '\n');

item = ' ';

}



cout << str2 << endl;



return 0;
}
Last edited on
Are you trying to replace only the fourth word with 'love', or every four letter word with 'love'?

Right now it looks like you're changing every four letter word to 'love', except if the last word in the string entered is four letters. I added in a couple of cout statements to the code to print the interim values below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Hello! Enter a string:

hate hate
item is h
word_length is 1
item is a
word_length is 2
item is t
word_length is 3
item is e
word_length is 4
item is  
word_length is 0
item is h
word_length is 1
item is a
word_length is 2
item is t
word_length is 3
item is e
word_length is 4
love hate



In a couple of else ifs I think you want the equality operator == instead of the assignment operator.
The following program utilizes the fact that if a string were to have a fourth word it'd be bounded by the third occurrence of ' ' on the left and any one of the following characters on the right “ !?;.” (add other characters if required):
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
#include <iostream>
#include <string>

std::string::size_type nthOccurrence(const std::string& haystack, const char needle, const size_t nth)
{
    std::string::size_type  pos {};
    size_t cnt{};

    while( cnt != nth )
    {
        pos = haystack.find(needle, pos);
        if ( pos == std::string::npos ){ return -1;}
        ++cnt;
        ++pos;
    }
    return pos;
}
void stringOfLove(std::string& haystack, const std::string::size_type pos)
{
    if (pos != -1)
    {
        auto posLove = haystack.substr(pos).find_first_of(" !?;.");//other other characters in required
        haystack.replace(pos, posLove, "love");
        std::cout << haystack << "\n";
    }
    else
    {
        std::cout << "The string doesn't have a 4th word to replace \n";
    }
}

int main()
{
    std::string haystackOne = "do you really hate chocolate?";
    std::string haystackTwo = "i am sam";
    std::string haystackThree = "this is it. spread the news";

    stringOfLove(haystackOne, nthOccurrence(haystackOne, ' ', 3));
    stringOfLove(haystackTwo, nthOccurrence(haystackTwo, ' ', 3));
    stringOfLove(haystackThree, nthOccurrence(haystackThree, ' ', 3));
}
OP: above program should be generalized a bit – suppose you've read an entire file into a std::string. Now also suppose the first paragraph of this file has just three words but the file (and hence the string) has more than four words. In this case the above program would not work as the fourth word is left bounded by the newline character, not ' '.
So, what we need to do here would be to find the third instance of isspace() (#include cctype) within the string, and not just the char ' ', which would also account for the newline character in this case. http://en.cppreference.com/w/cpp/string/byte/isspace
Accordingly, the nthOccurrence() function from the above post has been updated:
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <fstream>

std::string::size_type nthOccurrence(const std::string& haystack, const size_t nth)
{
    size_t cnt{};
    auto itr = haystack.begin();

    while( cnt != nth )
    {
        auto  pos = std::find_if(itr, haystack.end(), isspace);
        if ( pos != haystack.end() )
        {
            ++cnt;
            if(*itr != haystack.back())
            //last character of haystack isspace, itr cant be incremented hereon
            {
                itr = ++pos;
            }
            else
            {
                return -1;
            }
        }
        else
        {
            return -1;
        }
    }
    return std::distance(haystack.cbegin(), itr);
}
void stringOfLove(std::string& haystack, const std::string::size_type pos)
{
    if (pos != -1)
    {
        auto posLove = haystack.substr(pos).find_first_of(" !?;.");//other other characters in required
        haystack.replace(pos, posLove, "love");
        std::cout << haystack << "\n";
    }
    else
    {
        std::cout << "The string doesn't have a 4th word to replace \n";
    }
}

int main()
{
    std::string haystackOne = "do you really hate chocolate?";
    std::string haystackTwo = "i am sam";
    std::string haystackThree = "this is it. spread the news";

    stringOfLove(haystackOne, nthOccurrence(haystackOne, 3));
    stringOfLove(haystackTwo, nthOccurrence(haystackTwo, 3));
    stringOfLove(haystackThree, nthOccurrence(haystackThree, 3));

    std::ifstream inFile{"D:\\test.txt"};
    std::string haystackFour(std::istreambuf_iterator<char>{inFile}, {});
    stringOfLove(haystackFour, nthOccurrence(haystackFour, 3));
}
/* test.txt
I need money.
Hate is not what will fill my stomach.
*/
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// return start and end positions of next word at or after position 'from'
// return pair { str.size(), str.size() } if there is no next word
// word: longest sequence consisting solely of of alphanumeric characters
std::pair< std::size_t, std::size_t > next_word( const std::string& str, std::size_t from = 0 )
{
    // skip over leading non-alphanumeric character
    while( from < str.size() && !std::isalnum( str[from] ) ) ++from ;

    std::size_t end = from ;
    // skip to first trailing non-alphanumeric character
    while( end < str.size() && std::isalnum( str[end] ) ) ++end ;

    return { from, end } ;
}

// return start and end positions of nth word (n is zero based)
// return pair { str.size(), str.size() } if there is no nth word
std::pair< std::size_t, std::size_t > nth_word( const std::string& str, std::size_t n )
{
    auto pair = next_word(str) ;
    for( ; n > 0 ; --n ) pair = next_word( str, pair.second ) ;
    return pair ;
}

http://coliru.stacked-crooked.com/a/4d59761363b32784
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    const std::string str = "   zero/one/two \t (three) four+five\tsix!!!   7seven7  " ;
    std::cout << std::quoted(str) << '\n' ;

    const std::regex word_re( "\\w+" ) ;

    std::sregex_iterator iter( str.begin(), str.end(), word_re ), end ;
    for( int n = 0 ; iter != end ; ++iter, ++n )
       std::cout << "word #" << n << ". " << std::quoted( iter->str() ) << '\n' ;
}

http://coliru.stacked-crooked.com/a/de4fd2a56ed84ad8
std::regex method feedback:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <regex>
#include <iomanip>

int main()
{
    const std::string str = "colin's mum does hate chocolates" ;
    std::cout << std::quoted(str) << '\n' ;

    const std::regex word_re( "\\w+" ) ;

    std::sregex_iterator iter( str.begin(), str.end(), word_re ), end ;
    for( int n = 0 ; iter != end ; ++iter, ++n )
       std::cout << "word #" << n << ". " << std::quoted( iter->str() ) << '\n' ;
}
Topic archived. No new replies allowed.