Confused by over complicated example

Aloha Everybody,

I'm working on an exercise with the intention on practicing vectors. The point of the exercise was to make a program that would check to see if a word entered is a palindrome.

Because I'm a beginner programmer, I'm wondering if there is any benefit to this code being over complicated. It doesn't need the vector, or to rebuild the word character by character. If one just uses the reverse function already in the code, that is enough right? I tested it by just using the reverse function and returning that value and it works great! Is there any sense to this, or am I over simplifying something and missing the point? I just can't see the use of the vector at all...

Thanks in advance!

This is the answer given:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
string makeritem(string item) {
      string itemr = "";
      vector<string> hold;
      for(int i = 0; i < item.length(); ++i)
          hold.push_back(item.substr(i,1));
        reverse(item.begin(), item.end());
      for(int i = 0; i < item.size(); ++i)
        itemr += item[i]; 
        return itemr;
}

int main()
{
    string item, ritem;
    cout << "Enter a word: ";
    cin >> item;
    ritem = makeritem(item);
    if (ritem == item)
        cout << "The word is a palindrome."
             << endl;
    else
        cout << "The word is not a palindrome."
             << endl;
    return 0;
}

You are right, there is no point in using vector in this case.
This is inefficient in every way - in size, speed and simplicity.
Last edited on
Thank you for your reply, I can finally stop scrambling over this and get some sleep!

Much appreciated!
It's an exercise. It's meant to demonstrate things to you. This one demonstrates using vector.
Topic archived. No new replies allowed.