Menu

[Solved]Need Fix Function Basically Im Printing Phrase Like E X M P L E Printing Extra Space End L Q37088339

I need to fix this function.

Basically, im printing out a phrase like this: ” e x a m p le”

But its printing an extra space at the end like this: “e x a m pl e “

I need to get rid of the space at the end.

This is the note I have to fix it but I cant figure it out.

For phraseWithBlanks(), you need to change your for loop to havean index variable. That way you can only add a ” ” to the string ifyou are not at the end:

if (index + 1 < phrases.length())

{

    // adds a space between characters andpunctuation

    currentPhrase += ” “;

}

Here is what the function looks like now:

string phraseWithBlanks(const string& phrases, conststring& guessedRight)

{

// define variable

string currentPhrase;

// keeps looping as long as there is something in the phrase

for(char ch : phrases)

{

// if ch is a letter it is displayed as a blank, otherwise itis

// displayed as itself i.e. punctuation

if(guessedRight.find(tolower(ch)) == string::npos &&isLetter(ch))

{

currentPhrase += “_”;

}

else

{

currentPhrase += ch;

}

// adds a space between characters and punctuation

currentPhrase += ” “;

}

return currentPhrase;

}

Expert Answer


Answer to I need to fix this function. Basically, im printing out a phrase like this: ” e x a m p l e” But its printing an ext… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *