Word Replacement Bug

I am writing a program that is replacing words in vector<english> with vector<pirate> in a user input phrase. However, the first letter is always cut off on the pirate version (last line), such as input of "test" would output as "est". How do I fix this issue?
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
#include <iostream>
using namespace std;
#include <string>
#include <vector>

//Replaces specified phrase with replacement word
string replace(string english, string pirate, string phrase){
  string toReturn = "";
  int begin = 0;
  int startingIdx=-1;
  startingIdx = phrase.find(english, 0);
  //Searches for the english word and breaks the phrase up before the english word, adds the pirate word
  while (startingIdx!=-1){
    toReturn += phrase.substr(begin, startingIdx-begin);
    toReturn += pirate;
    begin = startingIdx+english.size();
    startingIdx = phrase.find(english, begin);
  }
  toReturn += phrase.substr(begin);
  return toReturn;
}

//Makes the first letter of the specified word uppercase
string ucfirst(string word){
  word[0]=toupper(word[0]);
  return word;
}

int main() {
  //Variables
  string newPhrase;
  vector<string>english{"hello", "friend", "madam", "officer", "stranger", "where", "is", "the", "my", "your", "restaurant", "hotel", "money"};
  vector<string>pirate{"ahoy", "matey", "proud beauty", "foul blaggart", "scurvy dog", "whar", "be", "th'", "me", "yer", "galley", "fleabag inn", "booty"};
  
  //User input
  cout<<"Enter your phrase: ";
  cin.ignore();
  getline(cin, newPhrase);

  //Making the pirate phrase, uppercase and lowercase
  for (int i=0; i<english.size(); i++){
    newPhrase=replace(english[i], pirate[i], newPhrase);
    pirate[i]=ucfirst(pirate[i]);
    english[i]=ucfirst(english[i]);
    newPhrase=replace(english[i], pirate[i], newPhrase);
  }
  cout<<"Pirate Version: "<<newPhrase;
}
Last edited on
On line 37 you ignore the first character. Remove this line and it should work.
I'd also put the using namespace std; after your includes, it's less confusing that way. And put some whitespace in there too, to separate the variable defs from comments, while loops, etc.
There's also issues re signed/unsigned numbers. Perhaps:

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 <vector>
#include <cctype>
using namespace std;

//Replaces specified phrase with replacement word
string replace(const string& english, const string& pirate, const string& phrase) {
	string toReturn;
	size_t begin {};

	//Searches for the english word and breaks the phrase up before the english word, adds the pirate word
	for (auto startingIdx {phrase.find(english, 0)}; startingIdx != string::npos; startingIdx = phrase.find(english, begin)) {
		toReturn += phrase.substr(begin, startingIdx - begin) + pirate;
		begin = startingIdx + english.size();
	}

	toReturn += phrase.substr(begin);
	return toReturn;
}

//Makes the first letter of the specified word uppercase
string ucfirst(string word) {
	word[0] = static_cast<char>(toupper(static_cast<unsigned char>(word[0])));

	return word;
}

string lower(string word) {
	for (auto& ch : word)
		ch = static_cast<char>(tolower(static_cast<unsigned char>(ch)));

	return word;
}

int main() {
	string newPhrase;
	const vector<string> english {"hello", "friend", "madam", "officer", "stranger", "where", "is", "the", "my", "your", "restaurant", "hotel", "money"};
	const vector<string> pirate {"ahoy", "matey", "proud beauty", "foul blaggart", "scurvy dog", "whar", "be", "th'", "me", "yer", "galley", "fleabag inn", "booty"};

	cout << "Enter your phrase: ";
	getline(cin, newPhrase);

	newPhrase = lower(newPhrase);

	//Making the pirate phrase, uppercase and lowercase
	for (int i = 0; i < english.size(); ++i)
		newPhrase = replace(english[i], ucfirst(pirate[i]), newPhrase);

	cout << "Pirate Version: " << newPhrase;
}



Enter your phrase: HEllo mADam Where IS the MONEY
Pirate Version: Ahoy Proud beauty Whar Be Th' Booty

Last edited on
Topic archived. No new replies allowed.