Asmaa Hadir
Asmaa Hadir

Reputation: 19

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?

Upvotes: 1

Views: 124

Answers (3)

Ruks
Ruks

Reputation: 3956

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

Upvotes: 0

6502
6502

Reputation: 114461

There are a couple of problems

  1. 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, and i=3). To fix this you need to change the final condition to use < instead of <=.

  2. 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] and word[2]. However your code uses length - i and for i=0 this will use word[3] that is outside the allowed limits for the word. You need to do the indexing using as formula length - 1 - i instead of length - 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.

Upvotes: 2

Shakil
Shakil

Reputation: 4630

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

Upvotes: 1

Related Questions