0

The first part of my code is used to receive a single string input from the user and replace certain characters in that word using string class member functions. This part was easy for me to figure out, but I can't seem to figure out how to form a new string out of these changed characters since I will need to use this string for further manipulation later on in my code.

This is problematic since the for loop outputs single char variables that can't be manipulated as a single string.

#include <iostream>
#include <string>

using namespace std;

int main(){
string word;
char letter;
int i;
cout << "Enter a word: ";
cin >> word;
cout << "\n";
    for ( i = 0; i < word.size(); i++)
    {
        if ( word.at(i) == 'e')
        {
            word.at(i) = '3';
        }
        if ( word.at(i) == 'i' )
        {
            word.at(i) = '1';
        }
        if ( word.at(i) == 'x' )
        {
            word.at(i) = '*';
        }
        cout << word.at(i);
    }
    cout << "\n";
}

As my code currently stands, a user might, for example, input the string "cheese" and receive the output ch33s3. However this output is not a string; it is an assortment of chars without a space to separate them. I can't continue my code any further with my for loop output remaining as it currently is.

Edit: I realize now that I already have what I need, but confused myself into thinking the scope wouldn't apply outside my for loop. Thanks for the quick and easy answers.

user3366885
  • 17
  • 1
  • 3
  • What exactly do you want the output to be? – David G Feb 28 '14 at 23:57
  • I want the output to remain as it is in appearance, just in the form of a string, rather than a bunch of chars. So in my example, I'd still want an output of ch33s3, but have this saved as a string. – user3366885 Mar 01 '14 at 00:00

4 Answers4

2

You were pretty much done already:

#include <iostream>
#include <string>

using namespace std;

int main(){
string word;
char letter;
int i;
cout << "Enter a word: ";
cin >> word;
cout << "\n";
    for ( i = 0; i < word.size(); i++)
    {
        if ( word.at(i) == 'e')
        {
            word.at(i) = '3';
        }
        if ( word.at(i) == 'i' )
        {
            word.at(i) = '1';
        }
        if ( word.at(i) == 'x' )
        {
            word.at(i) = '*';
        }
    }
    cout << word << "\n";
}
stackPusher
  • 6,076
  • 3
  • 35
  • 36
1

As it turns out, your work is already done for you. Your variable "word" would hold the value "ch33s3" after the loop ends.

stoopdapoop
  • 106
  • 5
0

The variable word contains the altered string and can be manipulated as such. It appears that you already have what you need.

Also - you may already know this - but you don't need the "at" function to accomplish what you're doing here. You can index the string like an array. For example:

word[i] = 'e';
cout << word[i];
plafratt
  • 738
  • 1
  • 8
  • 14
  • Just checked my code and you're exactly right, not sure why I didn't realize that myself. Thanks! – user3366885 Mar 01 '14 at 00:08
  • You're welcome. Could you select my answer as the correct one, please? Or at least "Vote Up" it if you can? – plafratt Mar 01 '14 at 00:20
  • @user3366885 _'I didn't realize that myself.'_ Though don't miss out the other answers explaining in more depth what's going on, and give points about your original intends and understandings. – πάντα ῥεῖ Mar 01 '14 at 00:37
0

However this output is not a string

Did you mean to use std::ostringstream instead of std::cout to receive the results?

The word.at(i) = xxx; statements already manipulated the word string, and you have it.

Here's a sample to show how to get a std:string result, without manipulating word directly:

#include <iostream>
#include <string>
#include <sstream>

int main() {
    std::string word;
    char letter;
    int i;
    std::cout << "Enter a word: ";
    std::cin >> word;

    std::ostringstream leetWord;
    for ( i = 0; i < word.size(); i++) {
        if ( word[i] == 'e') {
            leetWord << '3';
        }
        else if ( word[i] == 'i' ) {
            leetWord << '1';
        }
        else if ( word[i] == 'x' ) {
            leetWord << '*';
        }
        else {
            leetWord << word[i];
        }
    }

    // Here you can refer to the ostringstream::str() function to 
    // get a string
    std::string theWantedString = leetWord.str();
    std::cout << word << " => " << theWantedString << std::endl;
}

See the working sample.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190