-4

I get a String and if that String contains a palindrome i have to return the palindrome. For example : "hsjwiUHUkajs" should return UHU, "hjakhdANNAjhad" should return ANNA.

How can I do that?

public static void main(String [] args) {
    string s = "anna";
    for(int i = 0; i<s.length; i++;) {

    }
}
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
hamudi97
  • 11
  • 2
  • 3
    What **language** are you trying to do this in? This looks like both Java and C#? – Obsidian Age Feb 15 '18 at 23:17
  • 2
    Every string (of at least 1 char) contains a palindrome since every single letter (by itself) is a palindrome. Are you trying to find the longest palindrome? The first (more than 1 letter long) palindrome? or what? – twain249 Feb 15 '18 at 23:17
  • Have you tried to write down in words how you solve this yourself in your mind when looking at the string? How do you know whether or not the string has a palindrome that starts on the first letter? How you know whether or not the string has a palindrome that starts with the second letter? ... the third letter? ... – Dave S Feb 15 '18 at 23:23
  • Brute force way is to consider all the sequences possible from this string - and see if any of them is a palindrome. e.g. (1) char1 + char2, char1 + char2 + char 3 .... (2) char2 + char3, char2 + char3 + char4... (3)... – Ari Singh Feb 15 '18 at 23:24
  • Also please read [tour] then [ask] then [mcve], then edit or delete your question. SO is about answering programming questions, but is not a code-writing service. – Dave S Feb 15 '18 at 23:25
  • This question is not a duplicate of the linked question - OP wants to detect a palindrome anywhere inside a string. However, this question is off-topic as too broad anyway, so I won't reopen it. – Blorgbeard Feb 18 '18 at 02:19

1 Answers1

-1

The way is explode all characters in a array.

Make a var with start at 1. Make a for with compare 1# and #3 char. Ex: a#a (# = 2# position). If 1# = #3 then palindromo.

If not compare #2 = #3 If equal compare 1# = 4# If equal palindromo.

If not them inclement start at 2. Loop at for and compare 2# and #4 3# and #5