Substr help me

hello lets say we have string x = "5885";

we can get first two elements like string y = x.substr(0,2); or smth like that and if i want to get the elements from right to left like lets have a look at last two elements i can get them into variable like y = x.substr (from position 2,2elements) and after that i will have 85 in that y but i want to get it from the last element so i will go like y = x.substr(from x.length,2elements to the left) how do i do that ?

i hope you know what i mean

i want to save all elements from right to the middle of length and then to second variable from to right to the left until it reaches middle of the length

so later i can compare them if its palindrom
Last edited on
1
2
3
4
5
6
7
#include <algorithm>
#include <string>

...
string rhalf = x.substr(0, x.length() / 2);
reverse(x.begin(), x.end());
string lhalf = x.substr(0, x.length() / 2);


Here is how I would check for palindromes:

1
2
3
4
5
6
7
8
9
string x = "5885";
int s = 0, e = x.length() - 1;

while (x[s] == x[e] && s < e) {
    s++;
    e--;
}

return s == e;
smac thanks but its kinda Big task , while i was thinking how to create working algorithm i realised that my algorithm will work i just wanted to see that what you gave me that reverse , cuz its not just about palindromes , thank you :)
Topic archived. No new replies allowed.