Question

In: Computer Science

I wrote this program to check a string is palindrome or not. but in both cases,...

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;
}

Solutions

Expert Solution

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;
}


Related Solutions

C# Palindrome Permutation: Given a string, write a function to check if it is a permutation...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words. Input: Tact Coa Output: True (permutations: "taco cat", "atco cta", etc.) Comment your code to explain it.
Write a program that determines whether an input string is a palindrome; that is, whether it...
Write a program that determines whether an input string is a palindrome; that is, whether it can be read the same way forward and backward. At each point, you can read only one character of the input string; do not use an array to first store this string and then analyze it (except, possibly, in a stack implementation). Consider using multiple stacks. In Pseudocode please
Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
Write an X86-series assembly language program that checks whether input string is palindrome or not. A...
Write an X86-series assembly language program that checks whether input string is palindrome or not. A palindrome is a word, number, phrase or any other sequence which reads the same backward as forward e.g. madam, racecar. Sample Execution: Please enter a String: redivider The string is a palindrome Another Sample Execution: Please enter a String: abracadabra The string is not a palindrome
Palindrome Javascript I am trying to write this javascript function to check if a number is...
Palindrome Javascript I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. This is the code i have, but it doesnt work. Code: let convertButton = document.getElementsByClassName("btn")[0]; let userInput = document.getElementById("number").value; let results = document.getElementById("result").value; convertButton.addEventListener("click", function (event) { event.preventDefault(); console.log(userInput); if (validatePalidrome(userInput)) document.getElementById("result").innerHTML = "true"; else document.getElementById("result").innerHTML = "false"; }); function validatePalidrome(numbers) { let...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., 12321 This is the code i have, but it doesnt work. PLEASE MAKE SURE IT WORK BEFORE POST ANSWER, I GOT @ CODE THAT DID WORK BEFORE HTML JavaScript Palindrome Exercise rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" /> Palindrome Enter a positive number: Is this a palindrome? No...
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
C++: A palindrome is a string that is the same backward as it is forward. For...
C++: A palindrome is a string that is the same backward as it is forward. For example, “tot” and “otto” are rather short palindromes. Write a program that lets a user enter a string and that passes to a bool function a reference to the string. The function should return true if the string is a palindrome and false otherwise. When you do the judgment, capitalization, spaces, and punctuation should be neglected, that is, “Madam, I’m Adam” should test as...
word level palindrome program with stacks and queues I have a good portion of my program...
word level palindrome program with stacks and queues I have a good portion of my program finished, I am just trying to figure out how to do some final things to it. I want the program to only check for letters a through z and A = Z, treating uppercase and lower case as the same thing. I want it to ignore any other characters and to just realize word level palindromes, not sentence level, so for example a%345b@a c24&c8d)9cc...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT