help with "love" program

Hi, I'm trying to write a program that reads in a line of text and replaces each 4-letter word with the word "love". If the 4-letter word begins with a capital letter, then the word "Love" shall be used. My program seems to work okay if I use solely 4-letter words in my line of text, but I run into some problems outputting words that aren't 4 letters. Any help would be greatly appreciated! Here's what I've done so far:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <cctype>

using namespace System;
using namespace std;

const int SIZE = 40;

void intro();
// Explains program to user.

void get_input(string& input_line);
// Reads in input_line (line of text) from the user.

string replace_with_love(string input_line);
// Replaces each 4-letter word in input_line with the word "love"
// or "Love", the latter being used if the 4-letter word begins
// with a capital letter.



int main()
{
string input_line, love_string;

intro();

get_input(input_line);
love_string = replace_with_love(input_line);

cout << love_string << endl;

system("pause");
return 0;
}

void intro()
{
cout << "This program reads in a line of text and replaces\n"
<< "each occurrence of a 4-letter word with the word 'love'.\n"
<< "If the 4-letter word begins with a capital letter,\n"
<< "the word 'Love' shall replace it.\n\n";
}

void get_input(string& input_line)
{
string one[SIZE];
int i = 0;
string word;

cout << "Enter a line of text and press return\n"
<< "(no longer than 40 characters, including spaces\n"
<< "and punctuation): ";
getline(cin, input_line);


}

string replace_with_love(string input_line)
{
string one[SIZE];
string love_string(input_line);
string temp;
unsigned int location, location2, count = 0;
unsigned int pos = 0, last = 0;

last = input_line.find_last_of(" ");
while((pos < input_line.length()) && (count < SIZE))
{
location = input_line.find(" ", pos);
location2 = input_line.find(" ", location + 1);

if(pos == 0)
{
cout << "if" << endl; //check
one[count] = input_line.substr(pos, location);
count++;
pos = location + 1;
cout << "one[count] " << one[count-1] << endl; //check
}

if(last == location)
{
one[count] = input_line.substr((pos + 1), (location));
one[count+1] = input_line.substr(pos);
pos *= 10;
}

else if((location > (last + 10)) || (location2 > (last + 10)))
pos *= 10;
else
{
one[count] = input_line.substr(pos, ((location2) - (location + 1)));

}

pos = location2;
count++;

}
for(int i = 0; i < count; i++)
{
if(one[i].length() == 4)
{
temp = one[i];
if(isupper(temp.at(0)))
one[i] = "Love";
else
one[i] = "love";
}
}
for(int n = 0; n < count; n++)
love_string += one[n] + " ";
return love_string;
}
It is difficult when you don't use [code] tags.

Your replace_with_love() function doesn't actually do any replacing. It appends a bunch of stuff to the original string.

Also, you are using too many variables. The idea of searching using spaces, and keeping a previous, next index pair (which you call 'location' and 'location2') is a good one. Those two and the function argument (the string) is all you need.

Once you find a four letter word, all you need to do is actually change the string. You can use the replace() method of the string to do it.

Also, you don't need the 'love_string' variable, since the argument 'input_line' is not a reference. (You could just rename 'input_line' as 'love_string' and delete the string love_string(input_line); line.)

Hope this helps.
edits: I was away too long and was automatically logged out, so it took a minute to get things back the way I had them...
Last edited on
Hello I spent some time with your code and modified a few things Check it out

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
string replace_with_love(string input_line)
{
string one[SIZE];
string love_string(input_line);
string temp;
unsigned int location, location2, count = 0;
unsigned int pos = 0, last = 0;

last = input_line.find_last_of(" ");
while((pos < input_line.length()) && (count < SIZE))
{
location = input_line.find(" ", pos);
location2 = input_line.find(" ", location + 1);

if(pos == 0)
{
cout << "if" << endl; //check
one[count] = input_line.substr(pos, location) ;
count++;
pos = location;    //Modified
cout << "one[count] " << one[count-1] << endl; //check
continue; //Modified
}

if(last == location)
{
one[count] = input_line.substr((pos + 1), (location)) ;
one[count+1] = input_line.substr(pos);
pos *= 10;
}

else if((location > (last + 10)) || (location2 > (last + 10)))
pos *= 10;
else
{
one[count] = input_line.substr(pos+1, ((location2) - (location + 1)));

}
pos = location2;
count++;

}
for(int i = 0; i < count; i++)
{
if(one[i].length() == 4)
{
temp = one[i];
if(isupper(temp.at(0)))
one[i] = "Love";
else
one[i] = "love";
}
}
love_string.clear();
for(int n = 0; n < count; n++)
love_string += one[n] + " " ;
return love_string;
}


It worked for me!
Topic archived. No new replies allowed.