#include <iostream>
#include <string>
using namespace std;
string wordB(string input);
int main() {
//ask for word
cout << "Enter a word\n";
//get word
string input = "";
cin >> input;
//return with b in between all letters
cout << wordB(input);
cout << endl << input;
}
string wordB(string str) {
string rString = "";
for (unsigned i = 0; i < str.length(); ++i) {
rString += "B" + str.at(i);
}
cout << endl << rString;
return rString;
}
Trying to display the word a users enter where between every character there is the letter "B". When I run this with the word "join" I get back "trtr".
str.at(i)is an integral value."B" + nwherenis an integral value accesses thenth character of the string literal"B". Since the string literal"B"is represented as an array of twochar, any value ofnless than zero or more than1falls off the end. Your code appends the result of all that torString.