Palindrome

I am trying to check a string to be a palindrome. I have tried using for loop but I am looking for most efficient way.

Thanks for the help.
I've found the honor system to be sufficient.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
int main ()
{
    std::string usrInput;
    std::cout << "Type in a palindrome: ";
    std::getline(std::cin, usrInput);
    std::cout << '"' << usrInput << '"' << " is a palindrome.\n";

    return 0;
}
Type in a palindrome: racecar
"racecar" is a palindrome.


But seriously, can you show what you have so far? If it already works, what makes you think it isn't efficient enough?
booradley,

I don't understand your honor system comment.
This is what I have:

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

//Prototypes
bool IsPalindrome(string words);
void StringCap(string &word);

//Main Function
int main()
{

string word;
bool palin;

cout << "Enter a string: ";
getline(cin,word);
cout << endl;

StringCap(word);
palin = IsPalindrome(word);

cout << word;

if(palin)
{
cout << word << " is a palindrome.";
}
else
{
cout << word << " is not a palindrome.";
}

return 0;
}



bool IsPalindrome(string words)
{

string reverse;
int x=0;
int count=0;
int value=0;
bool palindrome;

palindrome = true;


for(int i=0; i < count/2; i++)
{

value = words[i];

reverse[i]=words[count-1-i];

reverse[count-1-i] = value;
}

for(int b=0; b < count; b++)
{

if (words[b] == reverse[b])
{
x=1;
}
else
{
x=2;
}

}

if (x==1)
{
palindrome = true;
}
else
{
palindrome = false;
}

return palindrome;
}

void StringCap(string &word)
{
int i;
int length;


length = word.length();

for( i=0; i < length; i++ )
{
word[i] = toupper(word[i]);
}

}

So many mistakes/tedious methods, I will focus on some mistakes in your IsPalindrome method:

1
2
3
4
int count=0;
...
for(int i=0; i < count/2; i++)
{ ... }


1
2
3
4
5
string reverse; // An empty string...
...
reverse[i]=words[count-1-i];

reverse[count-1-i] = value;


1
2
3
4
5
6
7
8
9
10
11
12
13
for(int b=0; b < count; b++)
{

if (words[b] == reverse[b])
{
x=1;
}
else
{
x=2;
}

}

If only the last characters match for both strings, your program will incorrectly report the string as being a palindrome
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <algorithm>
#include <string>

//does not handle mixed uppercase and lowercase letters.
bool isPalindrome(const std::string& string) {
	std::string reverse = string;
	std::reverse(reverse.begin(), reverse.end());
	return string == reverse;
}

int main(int argc, char* argv[]) {
	std::string string = "racecar";
	std::cout << "\"" << string << "\" is" << (isPalindrome(string) ? "" : "n't") << " a palindrome." << std::endl;
	std::cin.get();
	return 0;
}
Last edited on
Another way is to have a forward and a backward iterator or even just two position variables. Then one go up and the other go down until the right is now on the left (or they are equal).

Something like this is pseudo code:
1
2
3
4
5
6
7
8
9
10
left = position 0
right = last position

palindrome = true;
for left < right, left increment, and right decrement
    if(left != right)
        palindrome = false;
        break out of loop

now you know if it is a palindrome or not
Topic archived. No new replies allowed.