#include <iostream>
std::string get_string_from_to(char*, char*);
int main() {
std::string msg = "Hello, world!";
std::string new_msg = get_string_from_to(&msg[0], &msg[5]); /* the &msg[0] is necessary
* so that we pass a char* not a string */
std::cout << new_msg << "!\n";
return 0;
}
std::string get_string_from_to(char* from, char* to) {
std::string str;
int i = 0;
while (from < to) {
str.push_back(*from);
from++;
i++;
}
return str;
}
inb4 boost
By the way, I'm aware I'm posting this in the Lounge. It doesn't fit anywhere else because I'm not asking for help in the usual sense. This is just a fun (to me) and somewhat difficult problem (to me) to solve.
strcat(char* dest, const char* src) puts src onto the end of dest. I have a different problem. I want to do what the following pseudo-code would do if it were real:
1 2 3 4 5 6
function get from x to y (x, y)
location of x = &x
location of y = &y
in between = *(everything in between &x and &y)
string = x + in between + y
return string
If we pretend for a second that an std::string is a bunch of chars (which it sort of is, but in a cleverer way than it was implemented in C); then the variable at &x+1 should be the next character in the string at x.
I'm thinking this is hard to understand, so I'll make it easier. I'm not explaining very well.
Say we have the string msg = "Hello word". We pass the reference of H (&msg[0]) to as x get_from_x_to_y(x, y) and the reference of o (&msg[5]) as y. Then the return value of get_from_x_to_y should be "Hello".