convert 4 letter words to love

okay so im writing this program that is suppose to convert any 4 letter word in an input to love, Love if its the first letter of the sentence. For example if i input hate it'll return Love. the issue is i don't know how to get it to convert a whole string of words. it will only if you input for example hate, but wont accept hate hate

thanks.

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
#include <iostream>
#include <cstring>
#include <cctype>

using namespace std;

void newline();
void convert_love( char input_string[]);

int main()
{	
	const int size = 40;
	char input_string[size];
	char answer; 

	do
	{
		cout << "Enter a line of text: \n";

		cin.getline(input_string,40);

		convert_love(input_string);

		cout << "Do you want to run the program again?(Y/N) \n";
		cin >> answer;
		newline();
	}while ((answer == 'Y') || (answer == 'y'));

	system("pause");

	return(0);
}

void convert_love( char input_string[] )
{
	int i = 0;
	int count;
	char test_string[40];

	strncpy(test_string, input_string, 40);
	strlen(test_string);

	if((strlen(test_string) == 4) && (test_string[i] != '\0'))
	{
		strncpy(test_string,"Love", 4);
		i++;
	}

	cout << test_string << endl;
}

	
void newline()
{
	char symbol;

	do
	{
		cin.get(symbol);
	}while (symbol != '\n');
}
Last edited on
any help?
U could check the amount of chars before a space and if that is4 change to love
i was thinking the same thing but would i run it through a loop?
Why not use strings to hold the sentences and string::find() and string::replace() to only remove the word "hate" and replace with the word "love"? If you limit your program to replacing 4 letter words, then the word "what" can be replaced to say "hate". I cannot think of any useful applications of this method. You need to narrow the replacement criteria, and do so by using find and replace methods.

http://www.cplusplus.com/reference/string/string/find/

Last edited on
well i need to replace any 4 letter word with love, im not sure how to use string::find() and string::replace() since it hasn't been taught in class yet. Using a loop in the code i have above would work, im just confused om how too convert 4 letter words but also keep reading if there is a space

here is a better example of what the program is suppose to do

Original: Like Momma always said, "Be a good chil' and give me hugs".

Modified: Love Momma always love, "Be a love chil' and love me love".
Last edited on
I'm sure there is a much easier way to do this...but this seems to work for me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{ 
string input;
    vector<unsigned int> num(1);
    unsigned int numb;
    cout << "Please enter a sentence: " << flush;
    getline(cin, input);
    for(unsigned int i = 0; i<input.size()+1; i++)if(!isalpha(input[i]) || input[i] == NULL)num.push_back(i);
    for(unsigned int i = 0; i<num.size()-1; i++){
        numb = 0;
       // cout << "old: " << num[i] << endl;
        //cout << "new: " << num[i+1] << endl;
        for(unsigned int j = num[i]; j<num[i+1]; j++){
            if(isalpha(input[j])) ++numb;
            if(numb == 4 && j+1 == num[i+1] && num[i] != 0){ input[j-3] = 'l'; input[j-2] = 'o'; input[j-1] = 'v'; input[j] = 'e';}
            if(numb == 4 && j+1 == num[i+1] && num[i] == 0){ input[j-3] = 'L'; input[j-2] = 'o'; input[j-1] = 'v'; input[j] = 'e';}
        }
    //cout << "letters in word before positon " << num[i+1] << ": " << numb << endl; 
    }
    cout << input << endl;
}


Probably not the neatest or easiest way to code this but don't feel like making it pretty =p
Last edited on
Topic archived. No new replies allowed.