storing a string as a seperate vector of characters

Im writing a function that returns true if a wordis a palindrome or not. (spelled the same backwards and forwards like racecar and tacocat). My approach to this was to try and create a separate array of characters thatis reverse of the original string. So if they put in say word it takes word and creates a new array called b and goes backward from word. So itd be array b d,r,o,w. Then id try to compare the indexes and if the indexes are equal I return true. I just cant get it to do that though. I don't know how to begin at the end of word without using end() because I get errors when I use erase like I am in my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
#include <string>

bool ispalindrome(string &s);
//Returns true if s is palindrome

using namespace std;

int main() {


}

bool ispalindrome(string &s) {
    vector <char> b;
    for(int i = s.end(); i != 0; i--) {
        b[i] = s[i];
    }

}
Last edited on
Assigning a string to a vector:

http://www.cplusplus.com/reference/vector/vector/assign/

b.assign(s.begin(), s.end())


Reverse:

http://www.cplusplus.com/reference/algorithm/reverse/?kw=reverse


Since string is a container like vector you can use it like so:
1
2
string r_s = s;
std::reverse(r_s.begin(), r_s.end());
Topic archived. No new replies allowed.