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

8
  • 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!? Commented Apr 14, 2016 at 16:17
  • 1
    a[0]='0'+x. you should learn about en.wikipedia.org/wiki/ASCII Commented Apr 14, 2016 at 16:18
  • 1
    "it's not a char o.o" The std::string::operator[] overload in fact returns a char&. Commented Apr 14, 2016 at 16:22
  • Try char x = 'a'; x = 100; and see what happens. Commented Apr 14, 2016 at 16:25
  • 1
    Possible duplicate: Convert an int to ASCII character Commented Apr 14, 2016 at 16:30

3 Answers 3

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()) {...} 
Sign up to request clarification or add additional context in comments.

3 Comments

You should explain why this works. Also, why it is dangerous, and how to check the necessary prerequisite.
@Lincoln this worked however care to exaplin what BoBTFish was talking about? I'm intriqued
@3rdaccountQQ Edited my answer :)
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

1 Comment

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
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.

Comments