I have a palindrome set up in the system, and running fine.
However, when I type "never odd or even" or "A Man, A Plan, A Canal, Panama", it didn't run properly as if these words in quotation marks are considered palindromes. Where is the loophole on my code re: palindromes esp more than one word

//This palindrome project
//requires for a determination and provide a better understanding
//of such concept in C++ project.
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

int main()
{
   char word[30],rev[30],chr;
   cout<<"\t\tMy System.\n";
   cout<<"\t\t\tMy Version\n\n";
   do
   {
       cout<<"\nPlease enter word or number: ";
       cin>>word;

      int i, j;      
	  for(i = 0, j = strlen(word) - 1; j >= 0; i++, j--)          
		  rev[i] = word[j];        
		rev[i] = 0; 

       if(strcmp(rev,word)==0)
	  cout<<"The word "<<word<<" is considered a Palindrome.";
       else
		cout<<"The word "<<word<<" is not considered Palindrome.";

	   cout<<"\n\nDo you want to try again? Please press Y or N: ";
       cin>>chr;
   }
   while(chr=='y'||chr=='Y');
   
   cin.get ();
   return 0;
}

Recommended Answers

All 14 Replies

what if your sentence is longer than 29 letters? Thus you need to change "char word[30],rev[30]" to accompany this. That is why you should use std::string.

std::string word;
std::string reverseWord;
cin >> word; //word can be of any length, not like the char word[30] array you declared

//now do your logic just as it word were an char array

what if your sentence is longer than 29 letters? Thus you need to change "char word[30],rev[30]" to accompany this. That is why you should use std::string.

std::string word;
std::string reverseWord;
cin >> word; //word can be of any length, not like the char word[30] array you declared

//now do your logic just as it word were an char array

What do you mean about that? I mean where to put that string using the original code?

A fancier way to reverse the string can then be used:

std::string reversedString = std::string ( yourString.rbegin(), yourString.rend() );

You will also want yourString.compare(otherString) Give these revamps a shot and let us know how it turns out.

Dave

One of the problems with palindromes is the formatting. In this case you have commas and when you reverse the string they will not line up right but the letters in the sentence is still a palindrome. One solution would be to strip all punctuation and spaces from the sentence and then reverse it and compare. So you would get this as input.

A Man, A Plan, A Canal, Panama

Strip out everything that isn't a letter or number

AManAPlanACanalPanama

Reverse

amanaPlanaCAnalPAnaMA

Then you can use stricmp() to test if they are the same. stricmp() is case insensitive so you wont have to worry about making the input all one case. Here is a link about stricmp(): http://publib.boulder.ibm.com/infocenter/iadthelp/v7r0/index.jsp?topic=/com.ibm.etools.iseries.langref.doc/rzan5mst264.htm

>>A fancier way to reverse the string can then be used:

Another way is to use algorithms.

std::originalString = "1010101";
std::reversedString = std::reverse(originalString.begin(),originalString.end());

replace cin with cin.getline()

cin reads only till a ' '. So your sentence is not actually read properly...

Here is some code for testing palindromes:

bool isPalindrome(string s)
 {int i,l=s.length(); for (i=0; i<l/2; i++) if (s[i] != s[l-i-1]) return false; return true;}
int main(int argc, char *argv[]){
   string pal[] = {"able was I ere I saw elba", "Able was I ere I saw Elba", "Napoleon I noelopaN"};
   for (int i=0; i<3; i++){
     cout << "\"" << pal[i] <<  "\"  " << (isPalindrome(pal[i]) ? "is a " : "is not a ") << "palindrome" <<  "\n";
   }return 1;}

"able was I ere I saw elba" is a palindrome
"Able was I ere I saw Elba" is not a palindrome
"Napoleon I noelopaN" is a palindrome

Enjoy this snippet,
-- tesu

A fancier way to reverse the string can then be used:

std::string reversedString = std::string ( yourString.rbegin(), yourString.rend() );

You will also want yourString.compare(otherString) Give these revamps a shot and let us know how it turns out.

Dave

Where to post these code using the original code that I have?

>>Then you can use stricmp()
stricmp is not a standard function.

Well he could use _stricmp(). According to my MSDN _stricmp() is ISO conformant.

_stricmp is not available in all platform ( not on Linux )

Ah I see. Well I guess he could code it himself to make it portable.

Another option for checking if a word is a palindrome is using std::equal to check the string and std::isalnum + std::remoev_if to remove non alphanumeric characters.

e.g

#include <iostream>
#include <algorithm>
#include <string>
#include <functional>
#include <cctype>

int main()
{

	std::string palin;
	std::cin >> palin;
	
	//remove non alphanumeric characters
	std::string::iterator rem = std::remove_if(palin.begin(),palin.end(),std::not1(std::ptr_fun(&::isalnum)));
	palin.erase(rem, palin.end());

	//check if it's a palindrome
	if (std::equal(palin.begin(), palin.end(), palin.rbegin()))
		std::cout << "Palindrome" << std::endl;
	else
		std::cout << "Not a palindrome" << std::endl;

	return 0;


}
#include <iostream>
#include <string>

using namespace std;

int main()
{
char str[100];

cout << "Enter word :";
cin >> str;
int x = strlen(str)-1;
for(int i = 0; i <= x; i++)
{
if (str[i] == str[x-i])
{
continue;
}
else
{
cout<<"Not a palindrome"<<endl;
return 0;
}
}

cout << "Indeed Palindrome"<<endl;
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.