Click here to Skip to main content
15,883,948 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys, I just wanted to know if the current logic palindrome is valid or not since I have seen so many other ways to solve such kind of problem. All I am asking if it is valid for me to stick with this method or not (maybe has some problems trying other examples...).

As for the runtime, it worked fine for me so far.

Code:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int length = 0;
    char input[80]; string res;
    cout<<"Please enter a string: ";
    cin.getline(input, 80);
    
    for (length = 0; input[length] != '\0'; length++);
    
    for (int i = length - 1; i >= 0 ; i--)
    {
        res += input[i];
    }
    
    if (input == res)
    {
        cout<<input<<" is a palindrome!"<<endl;
    }
    else
    {
        cout<<input<<" is NOT a paindrome!"<<endl;
    }
    return 0;
}
Posted
Updated 14-Oct-14 12:36pm
v2
Comments
Sergey Alexandrovich Kryukov 14-Oct-14 18:53pm    
Yes, there are problems... please see Solution 1.
—SA

1 solution

This at this: what happens if a user enters more than 80 characters? This is a serious problem, you have to fix it. With the type std::string, you don't have to use the char[] type at all and care about it. If you choose to use std::string, input it, not null-terminated string. Also, for std::string, you don't need to check up null character: use string::length() instead. Please review this class thoroughly: http://www.cplusplus.com/reference/string/string[^].

Also, you repeat "80" twice. This is not supportable. Define it as a constant explicitly.

You can use and compare string values, but this is not efficient. (Yes, I understand that performance is not critical at all, but aren't you do it for learning programming? This is a good reason to try to do the very best.) Here is what you can do: using a constant string, just compare two characters in a loop, starting from leftmost and rightmost character, until you reach the middle. It should work for both odd and even lengths, as well as empty string (and empty string is a palindrome). This way, you can break from the loop at first discrepancy you account, without checking up all the characters.

—SA
 
Share this answer
 
Comments
Garth J Lancaster 14-Oct-14 19:29pm    
haven't thought about it (or done it before), but the loop [check leftmost and rightmost characters] etc seems like a sweet way of solving it
CubbiMew 14-Oct-14 20:11pm    
It's even sweeter if you realize that this loop can be done with a single call to std::equal
Garth J Lancaster 14-Oct-14 20:18pm    
as in bisect the string into two equal parts (possibly ignoring a 'middle' character) and compare ? surely you'd have to reverse one of the bisections ?
CubbiMew 14-Oct-14 20:23pm    
Surely: std::equal(s.begin(), s.begin() + s.size()/2, s.rbegin())
Garth J Lancaster 14-Oct-14 20:28pm    
d'oh - yes .. s.rbegin() .. reverse iterator

well done

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900