String Help

how do you move the last 3 values in a string of unknown length to the beginning and add a #. for example: hello would become #llohe
Last edited on
You can do this with the std::string::substr function that C++ offers.

http://www.cplusplus.com/reference/string/string/substr/

You are first given a string of unknown length n. Rearrange your string such that the last three characters of the string are on the front of the string, then you can declare a new value for the string by doing basic string operations using the + operator.

1
2
std::string myString = "Hello";
myString = "#" + myString;
or putting that together you get:
mystring = "#";
mystring += orig.substr(part1 params) + orig.substr(part2 params);
you may need to get its length (orig.length()) to do this. The length of a string is never really unknown. If you are not allowed to get it for this exercise, you may have to do something screwy to get the result you want. or you may be able to do it with just .end() etc.
Last edited on
Topic archived. No new replies allowed.