1

I have a string a = "Hello world!", now for fun I wish to change the first letter in the string a to a number of my choosing. for example

string a = "Hello world!";
int x;
std:cin>>x;
a[0] = x;
cout << a;

Now what i want it to produce is "xello world!" x being the number i typed in, but instead I get a little smiley face.

Anyone seen anything similar and know how to fix it?

Also: Why can you even access a string like this string[] ? it's not a char array o.o

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    What if the decimal representation of the number is more than 1 digit long? For instance, you enter `123`, do you want the result to be `123ello world!`? – BoBTFish Apr 14 '16 at 16:17
  • 1
    `a[0]='0'+x`. you should learn about https://en.wikipedia.org/wiki/ASCII – fukanchik Apr 14 '16 at 16:18
  • 1
    _"it's not a char o.o"_ The [`std::string::operator[]`](http://en.cppreference.com/w/cpp/string/basic_string/operator_at) overload in fact returns a `char&`. – πάντα ῥεῖ Apr 14 '16 at 16:22
  • Try `char x = 'a'; x = 100;` and see what happens. – Christian Hackl Apr 14 '16 at 16:25
  • 1
    Possible duplicate: [Convert an int to ASCII character](http://stackoverflow.com/questions/4629050/convert-an-int-to-ascii-character) – NathanOliver Apr 14 '16 at 16:30
  • @BoBTFish I've used module to find out how many digits long the number is so no problem there :) –  Apr 14 '16 at 17:13
  • @fukanchik Yes this worked thanks :) –  Apr 14 '16 at 17:14
  • _"Why can you even access a string like this string[] ? it's not a char array"_ Well, no, it pretty much is, and its interface is deliberately constructed such that you can use it as one, because that's really useful and sensible. – Lightness Races in Orbit Apr 14 '16 at 17:46

3 Answers3

5

If x is a single digit number, do this instead:

a[0] = x + '0';

EDIT:

Why this works:

Each element in string is a char. A char is represented by an integer. You can check this up in the ASCII table. For example, integer 48 represents char '0', integer 49 represents char '1', etc. Thus, by adding your integer x to char '0', you can get the digit 'x' that you want.

Why it might be dangerous:

One reason that I could think of would be when writing to unallocated memory in an empty string, which might invoke undefined behavior.

You can then check if a string is empty with string::empty(). In this case,

if (!a.empty()) {...}
Lincoln Cheng
  • 2,263
  • 1
  • 11
  • 17
3

Using string::replace and std::to_string, this will work regardless if x is a single or multiple digit number:

string a = "Hello world!";
int x;
std:cin>>x;
a.replace(0, 1, std::to_string(x));

Live Example

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • This is very intresting, I'm sure I'll use it in the future, however the optimal solution for my problem seems to be what Lincoln wrote –  Apr 14 '16 at 17:19
0

As an additional answer you could use the at() method in a try block as it is performing bounds checking and throws exception of type std::out_of_range if the access is invalid:

If you add x + '0' and x is higher than 9 it won't give you a number.
Look at the ascii table

int main()
{
    std::string a { "Hello World" };
    int x {};
    std::cin >> x;
    try {
        a.at(0) = '0' + x;      // OOPS: '0' + 10 will give you ':'
        std::cout << a << '\n';
    }
    catch(std::out_of_range& e) {
        std::cout << e.what();
    }
}

A better solution is what @PaulMcKenzie answered, to use std::to_string and string::replace.

Andreas DM
  • 10,685
  • 6
  • 35
  • 62