-1

Code

#include <iostream>
#include <string>
using namespace std;

bool isPalindrome(string str)
{
    for(int i = 0; i <= str.length()-1; i++)
    {
        if(str[i] != str[str.length()-1-i])
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}

int main()
{
    string text;

    do
    {
        cout << "Enter some Text: " << endl;
        cin >> text;
        if(isPalindrome(text))
        {
            cout << "The text is a palindrome" << endl;
        }
        else
        {
            cout << "The text is not a palindrome" << endl;
        }

    }while(text != "Q");

    return 0;
}

Input_1

otto

Output

The text is a palindrome

Input_2

 ottop

Output

The text is a palindrome

Input_3

ottopo

Output3

The text is a palindrome

I want to check the string entered from user is palindrome or not ? Why Output for my 3rd input is getting wrong ? I think I am missing something.

Abhishek Mane
  • 619
  • 7
  • 20
  • 1
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Oct 02 '16 at 23:36
  • 2
    `return` returns from a function. – melpomene Oct 02 '16 at 23:37
  • 1
    For one thing, `return` returns from your function. That means your loop only runs the first iteration, the `return` statements cause it to return during that iteration and it'll never go any further than the first/last character comparison. – Todd Knarr Oct 02 '16 at 23:42

4 Answers4

4

You are returning true on the first iteration of the loop. And you only need to iterate on half the string.

Corrected:

    size_t len = str.length();

    for(int i = 0; i < len/2; i++){
        if(str[i] != str[len-1-i]){
            return false;
        }
    }
    return true;
selbie
  • 100,020
  • 15
  • 103
  • 173
0

You don't have to have a return true after the else statement there. Put that outside the for loop and your code should work.

As an extra nitpick, you don't have to check until strlen(s) - 1, but strlen(s) / 2 is actually enough. I'll leave figuring that out to you, as it's quite straightforward and not too different from what you are doing, at all.

SenselessCoder
  • 1,139
  • 12
  • 27
0

As it has been said, the return true; should not be in the else, but after the end of the for loop. This can also be accomplished in just a few lines. A string is a palindrome if it is the same reversed as it is normally. All you need is

reversedText = reverse(text.begin(), text.end());
if (text == reversedText)
    //indicate that it is a palindrome
else
    //indicate that it is not a palindrome`

Just be sure to include algorithm. I can understand if you don't want to use a library for some reason, but I just want to make sure that you know that this can be accomplished very easily.

TGrossb
  • 44
  • 6
  • thank you for your answer! I know it could be done very easily with library functions, but sometimes it's good practice to do it the "hard" way! –  Oct 03 '16 at 07:05
  • @BamBam281189 I understand that. I've done similar things many times. I believe it helps me to get to know the language better. – TGrossb Oct 03 '16 at 15:43
-1

You should set a flag to 1 if it is not a palindrome, then break after:

for (int i = 0; i < len/2; i++) {
    if (str[i] != str[len-1-i]) {
        // here .......
    }
  • Just don't give answers if you want to. Put some effort see what mistake done by user who asked Question also go through given answers by other users & if you still find that you are contributing something better and different from other answers then answer. – Abhishek Mane Dec 26 '21 at 04:26