In: Computer Science
I wrote this program to check a string is palindrome or not. but in both cases, it gives me palindrome.
could someone help me with this program? it has an error I couldn't find.
when I run the code and give a string, in both cases it gives me palindrome. for example if I give Pop it says it is a palindrome but if I give Salima, it also says palindrome.
#include<string>
#include <iostream>
using namespace std;
class PString:public string
{
private:
string testWord;
public:
PString();
PString(string
Pword){setPword(Pword);}
void setPword(string Pword){
testWord = Pword;}
bool isPalindrome(string
testWord){
}
};
bool isPalindrome(string strg)
{
for (int count = 0; count < strg.length()-1;
count++)
{
if (tolower(strg[count]) !=
tolower(strg[strg.length()-(count+1)]))
{
return
false;
}
else
{return true;}
}
}
int main ()
{
// Request input from user
string str;
cout << "\nThis is a palindrome-testing program."
<<endl;
cout<<endl;
cout<<"Enter a string to test:"
<<endl;;
cin >> str;
// Create a PString object that will check strings
PString s(str);
// Check string and print output
if (s.isPalindrome(str))
cout << s << " is a palindrome.";
else
cout << s << " is not a palindrome.";
cout << endl;
return 0;
}
There are two problems with the above code:
1. if (s.isPalindrome(str)) -> is not the correct way to call the isPalindrome function. The code doesn't go inside the IsPalindrome function. Instead use ->if (isPalindrome(str))
2. There is a problem with the return statement inside the for loop in isPalindrome() code.
Lets understand this with an example. For eg: the String is "PopuP"
When we go inside the for loop,
first element=last element, so it will automatically return true without comparing the rest of the elements.
We need to handle the return statement like below:
Here instead of return true on comparing a single element we will return true, only after all the elements have been compared and are equal to their counterpart element. If any of them is not equal, it will return false there itself within the for loop.
Find the output below for this code:
Also find the complete code for your reference below after corrections:
#include<string>
#include <iostream>
using namespace std;
class PString:public string
{
private:
string testWord;
public:
PString();
PString(string Pword){setPword(Pword);}
void setPword(string Pword){ testWord = Pword;}
bool isPalindrome(string testWord){
}
};
bool isPalindrome(string strg)
{
for (int count = 0; count < strg.length()-1; count++)
{
if (tolower(strg[count]) !=
tolower(strg[strg.length()-(count+1)]))
{
return false;
}
// else
// {return true;}
}
return true;
}
int main ()
{
// Request input from user
string str;
cout << "\nThis is a palindrome-testing program."
<<endl;
cout<<endl;
cout<<"Enter a string to test:" <<endl;;
cin >> str;
// Create a PString object that will check strings
PString s(str);
// Check string and print output
if (isPalindrome(str))
cout << s << " is a palindrome.";
else
cout << s << " is not a palindrome.";
cout << endl;
return 0;
}