undesired output of the Palindrome program using C++
So I started learning C++ two weeks ago and I want to build a program that checks if a string is a palindrome or not. I tried different ways including the str1==str2 method in the following way:
#include<iostream> #include<string> using namespace std; string empty; string word; bool inverse(string word) { for (int i=0;i<=word.length();i++) { empty+=word[word.length()-i]; } return empty==word; } int main() { cout<<inverse("civic"); }
The output is always 0
Second way: the str1.compare(str2) method
#include<iostream> #include<string> using namespace std; string empty; string word; bool inverse(string word) { for (int i=0;i<=word.length();i++) {empty+=word[word.length()-i];} if (word.compare(empty)) return true; else return false; } int main() { if (inverse(word)==true) cout<<"is a palindrome"; else cout<<"is not a palindrome"; cout<<inverse("ano"); cout<<inverse("madam"); }
the output is always: is palindrome1 (with 1 or two ones at the end of "palindrome") even if the string is not a palindrome.
please explain to me what mistakes I made and how I can correct them. Also If I want to make my program handle a string that has white space in it, how can I do it?
There are a couple of problems
Your code is looping too many times. For example a word of three letters should loop three times, but your code loops for 4 (
i=0
,i=1
,i=2
, andi=3
). To fix this you need to change the final condition to use<
instead of<=
.You are computing the symmetrical index with the wrong formula. If for example you have a word of length three the letters be
word[0]
,word[1]
andword[2]
. However your code useslength - i
and fori=0
this will useword[3]
that is outside the allowed limits for the word. You need to do the indexing using as formulalength - 1 - i
instead oflength - i
.
Both of these errors are quite common in programming and they're called "off-by-one" errors. Remember to always double-check the boundary conditions when you write code so that you can keep this kind of error away from your programs.
Example #3 – Program to Check Palindrome in C using a do-while loop A do-while loop is a kind of similar to a while loop, But in the do-while loop, the loop gets executed at least one time. In the Do While loop , The condition appears at the end of the loop, so the statements in the Do loop executed before checking whether a condition is true
For first one you need to change
for (int i=0;i<=word.length();i++) {empty+=word[word.length()-i];}
to this
for (int i=0;i<word.length();i++) {empty+=word[word.length()-(i+1)];}
Check if a string is palindrome in C using pointers; C Program to Check if a Given String is Palindrome; Check if a given string is a rotation of a palindrome; Check whether the given string is Palindrome using Stack; Check if there exists any sub-sequence in a string which is not palindrome; Java program to check whether a string is a Palindrome
Your program's behavior will become undefined after this line:
for (int i = 0;i <= word.length(); i++) empty += word[word.length() - i];
Since length is always one plus the last element (Since the first index is zero), when i
is 0
, then: word[word.length()]
will give you the element after the last element, which is not possible and thus your program will invoke undefined behavior since C/C++... word[word.length()]
is also possible when i
itself becomes word.length()
, so change <=
(less than or equal to) to <
(less than)
So, it should be:
for (int i = 0;i < word.length(); i++) empty += word[word.length() - 1 - i];
The function computerPowers() in the program computes the powers of 101 using dynamic programming. Overflow Issues: As, we can see that the hash values and the reverse hash values can become huge for even the small strings of length – 8. Since C and C++ doesn’t provide support for such large numbers, so it will cause overflows.
Preprocess/Prepare the string to use Manachers Algorithm, to help find even length palindrome, for which we concatenate the given string with itself (to find if rotation will give a palindrome) and add ‘$’ symbol at the beginning and ‘#’ characters between letters. We end the string using ‘@’.
If the sum is not a palindrome, repeat this procedure until it does. Write a program that takes number and gives the resulting palindrome (if one exists). If it took more than 1,000 iterations (additions) or yield a palindrome that is greater than 4,294,967,295, assume that no palindrome exist for the given number. Examples:
A palindrome is a word, number, phrase, or other sequences of characters which reads the same backward as forward. Words such as madam or racecar or the number 10801 are a palindrome. For a given string if reversing the string gives the same string then we can say that the given string is a palindrome. Which means to check for the palindrome, we need to find whether the first and last, second and last-1, and so on elements are equal or not.
Comments
empty+=word[word.length()-i];
will invoke Undefined behavior wheni
is zero...- Your for loop is checking i <= word.length(), while it should check i < word.length()
- oh I understand, I will make sure to check for the boundaries thank you so much for your help!