Replacing four-letter words with "love"

Hello. I get "Microsoft Visual C++ Runtime Library" error while trying to debug this code with Visual Studio 2013. It happens only if the last symbol is punctuation mark.

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
#include<iostream>
#include<string>
using namespace std;
int main()
{

	string input;
	
	getline(cin, input);
	for (int i = 0; i < input.length(); i++)
	{
		if (!(isalpha(input[i])) && !(isalpha(input[i + 5])) &&isalpha(input[i + 1]) && isalpha(input[i + 2]) && isalpha(input[i + 3]) && isalpha(input[i+4]))
		{
			input[i + 1] = 'l';
			input[i + 2] = 'o';
			input[i + 3] = 'v';
			input[i + 4] = 'e';
//Also, is there any way to transform the above code to a less-ugly one?
		}
	}
	if (input[0] == 'l') input[0] = 'L';




	cout << endl << input << endl;

	system("Pause");
	return 0;
}
Last edited on
I think you should start with checking if the word you enter is a four letter word, and if it is, then you could have a for loop to replace the word with 'love'. You could also put a condition in there that checks if there is punctuation in your string, and to have it ignore it.
I think you should start with checking if the word you enter is a four letter word

It is not just a word, but a text.
Topic archived. No new replies allowed.