-4

Possible Duplicate:
Write a function that returns the longest palindrome in a given string

I have a C++ assignment which wants me write a program that finds the longest palindrome in a given text. For example, the text is this: asdqerderdiedasqwertunut, my program should find tunut in the index of 19. However if input is changed into this astunutsaderdiedasqwertunutit should find astunutsa in the index of 0 instead of tunutin index of 22.

So, my problem is this. But I am a beginner at the subject, i know just string class, loops, ifs. It would be great if you could help me on this.

Thanks in advance.

Community
  • 1
  • 1
rigormort
  • 19
  • 2
  • 4
    If you don't even know where to start, perhaps you should go ask whoever set you the assignment. – James M Apr 13 '12 at 00:04
  • Actually I am troubling in stage of forming an algorithm, so unfortunately I don't have any algorithm so far. – rigormort Apr 13 '12 at 00:05
  • 3
    @MerveÇakır Start with an algorithm to decide if a given string is a palindrome. – twain249 Apr 13 '12 at 00:06
  • @twain249 I am now able to find if a given string is a palindrome or not, but only if it is a word as we now it. In this case the text is something complex and palindrome should be inside somewhere in the text. – rigormort Apr 13 '12 at 00:08
  • @MerveÇakır can you post that code. The point of SO is to help find whats wrong with something, not write the algorithm for you. – pstrjds Apr 13 '12 at 00:09
  • My intention was not you to ask the code for me, a simple inspiration could work as it did now. Thanks for @dasblinkenlight for the help. – rigormort Apr 13 '12 at 00:15
  • 1
    Also thanks @grc. That link is very useful. I couldn't find it when I searched for existing questions. – rigormort Apr 13 '12 at 00:16
  • 1
    so I am supposed to do your homework for you so you can get an A. Then you will come interview with my company and fail every questions -- waste my time! – kasavbere Apr 13 '12 at 00:34

2 Answers2

3

The idea is very simple:

  • Write a function is_palindrome(string) that takes a string, and returns true if it is a palindrome and false if it is not
  • With that function in hand, write two nested loops cutting out different substrings from the original string. Pass each substring to is_palindrome(string), and pick the longest one among the strings returning true.

You can further optimize your program by examining longest substrings ahead of shorter ones. If you examine substrings from longest to shortest, you'll be able to return as soon as you find the first palindrome.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Dasblinkenlight's idea is pretty good, but it's faster this way:

A palindrome has either an even number of letters or odd, so you have two situations. Let's start with the even. You need to find two consecutive identical letters, and then check whether the immediately previous letter is identical to the next letter. The same in the other situation, except at first you only need one letter. I don't speak English that well, so I hope you understood. :)

Adina T.
  • 21
  • 2