![]() int i 0 int j myString.length () - 1 boolean isPalindrome. To reverse the number: keep dividing it with 10 until the result is 0. If it is, then string is a palindrome, otherwise its not. You could also reverse the number and check if the reversed one and your number are equal. After the loop ends, check if the value of the boolean variable is still true. Return new StringBuffer(curr).reverse().toString(). Inside the loop, in any iteration, if the if condition evaluates to true, set that boolean variables value to false and break out of the loop. in this case Īlso this program currently results in: String index out of range: -1 public static String findAllPalindromes(String input) This comparison will determine if the string is a palindrome. Then using an if else statement which will check to see if the c stack which holds the original input is equal to the reverse stack. Introduction In this article, were going to see how we can check whether a given String is a palindrome using Java. The final output should be a string that includes all palindromes. Reversing the characters in the cstack (Which should hold the users input) into a new stack called rstack using another foreach loop. so the next palindrome should read "alla" I need to change the code so that once it finds the first largest palindrome ("abba") the start of the loop will take place after that substring. Once it reaches the max length of the word it iterates the start of the substring and repeatsĮxample: check palindrome "b" "bb" "bba" and so on. It loops through creating a substring that changes as the loop progresses.Įxample: checks palindrome "a" (true) "ab"(false) "abb" (false) "abba" (true) and so on. For example, the longest palindromic substring of 'bananas' is 'anana'. But when I submit in SPOJ it showsss wrong. PM77-1 I runned code on Onlinegdb and Ideone, I get correct ans. Run your code under debugger step-by-step and check all relevant variables. Palindrome class doesnt need to know the input as member or for initialization purpose. Stack Overflow Public questions & answers Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers Talent Build your employer brand. Write an Java method longestPalindrome that given a string s, it returns the longest palindromic substring. In SPOJ, the first two test cases are compiling but for the next one and onwards its showing the wrong answer. Third, its not a good practice to do everything une constructor. Second, when you reverse the input using stack, you push the index on the stack, not the character. I would also like any constructive feedback on the way I have written the code, as I do not want to pick up any bad habits.I need to find all the palindromes in a string. First, you need to give the input to the Palindrome class. A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction. A few times before I was told I did not put enough code in. Anyone want to help out a newbie who is stuck? I was unsure if the whole program needed to be displayed, if not I am sorry. ![]() Ironically the teacher solution uses Character. I can remove the code form the display() method and put it in the check() method and it runs perfectly. This is the programming assignment from the Java Software Solutions (PP3.11) that I assign my students. ![]() I am not calling it properly or something. The program runs fine, it just returns the wrong message. OK, I know I am missing something easy here, however I cannot find my mistake. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |