-1

I am trying to get a string a want to change the all the characters to the "X", this is where i reached till now.

#include<iostream>
#include<string>
#include<cctype>

using namespace std;

int main() {
    string line;
    getline(cin, line);
    for (decltype(line.size() index = 0; index != line.size(); ++index)) {
        line[index] = "X";
    }
    cout << line << endl;
    return 0;
}

I want to change all the characters to "X" . Please Help.

Updated Code:

#include<iostream>
#include<string>
#include<cctype>

using namespace std;

int main() {
    string line;
    getline(cin, line);
    for (decltype(line.size()) index = 0; index != line.size(); ++index) {
        if (isspace(line[index])) {
            line[index] += 1;
            continue;
        }
        else {
            line[index] = 'X';
        }
    }
    cout << line << endl;
    return 0;
}

It prints ! in the space between strings, how to solve that.

ajknzhol
  • 6,322
  • 13
  • 45
  • 72
  • 1
    Wrong parentheses, and `"X"` should be `'X'`. Other than that it should work. – Jon Jan 26 '14 at 11:21
  • No, if you have another question, post it as another question. – StoryTeller - Unslander Monica Jan 26 '14 at 11:37
  • What do you mean by "string" in your question? Does your string may have blank space(s)? Do you want to replace the blank spaces by 'X' as well or do you want to only replace alphanumeric characters? What do you want to achieve by this line: line[index] += 1; – qqqqq Jan 08 '16 at 18:43

5 Answers5

6

Use the fill constructor

line = std::string(line.size(), 'X');

In c++11 the internal buffer will be moved, and not copied.

But if you want to avoid the allocation of a new buffer (along with the discarding of the old one), you'd want to go along with one of the other answers.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
3

for(decltype(...)) interessting construct you have come up with here.

You though might want to try this one:

for (char& ch : line)
{
    ch = 'X';
}

or this one:

for(unsigned int i = 0; i < line.size(); i++)
{
    line[i] = 'X';
}

Also notice that there is a difference between 'X' and "X": The first one is a char literal, the second one a string literal (which means the return type is const char[2] opposed to const char)

Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77
  • 2
    An algorithm is self documented when writing code, even if here, the range based for loop is so simple it is OK too `std::fill( begin(line), end(line), 'X' );` – galop1n Jan 26 '14 at 11:22
  • @galop1n There are many ways to reach a goal, but I think its more interessting for the OP how to do it the way he wanted to do it, as he apperently lacks the knowledge to iterate over a collection in general. – Sebastian Hoffmann Jan 26 '14 at 11:23
2

using a range based for loop with auto specifier (see also this answer):

for (auto& i: line)
   i = 'X';
Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

Yet, another way to do it:

std::for_each(line.begin(), line.end(), [](char& c) { c = 'X'; } );

you could even use decltype here:

std::for_each(s.begin(), s.end(), [](decltype(s[0]) c) { c = 'X'; } );

but honestly, for simple things just use simple loop

marcinj
  • 48,511
  • 9
  • 79
  • 100
1

It's always good to search first for standard library functions that already do what you're trying to do. In this case, std::string.replace will do the trick:

#include<iostream>
#include<string>

using namespace std;

int main() {
    string line;
    getline(cin, line);
    line.replace(0, line.size(), line.size(), 'X');
    cout << line << endl;
    return 0;
}
Edward
  • 6,964
  • 2
  • 29
  • 55