Thank you all to those who contributed - I'm learning a lot about what can and cannot be done. Borrowed part from a Java forum where someone was doing the same exercise, improved it with a random number generator, made the range of ints to fill make sense for the project, and added counters to total the number of palindromes. I ended up completely dumping the code and going a different route. ("Enter in what you want Palindromeinated => ") I found this code that I could make generate 00000 - 99999 by adding two chars, and making all of the ranges 0 - 9. They are used to change one dimensional chain into 2 or 3 dimensional structure. This may surprise you because there are no nested loops in your code, but both the substring method and the + operator for Strings require the creation of a new String, which requires copying its contents. Palindromes are used in DNA for marking and permitting cutting. If n is the length of the input string, your code takes O(n2) operations. For example there are researches about biological sequence compression algorithms, that use this property. For eg: 121 - >12110 - >1 Then 121/10 -> 12 Similarly keep doing this till its modulus is not zero Then multiply the highest number with (len of digit -1) like 1100+210+1 If it is same then it is palindrome I know its quite long but its the simplest thing Share. For this one, I wanted to do it all in arrays, to learn something new. In real life, could be used for some compression algorithms. This will be the reverse of the original. Now, since the LAST character of the string is on top, you just need to pop until the stack is empty and store it in a string. So the resulting stack would look like this: TOP -> tset <- BOTTOM. In simplest words, a string is palindrome if it is equal to it’s reverse string. string s 'test' for i0 to s.length stack->push(si) This would push t->e->s->t from left to right. I made a method for a previous assignment that used a for loop to output it in reverse. Learn to check if a given string is palindrome string with simple java programs using stack, queue or simple loops. I would still try your ideas and let you guys know.I need to make an array, and fill it with 25 5-digit palindromic numbers. i was asking the user to enter the number in the form of an arry but i was not actually entering the digits in the number one by one instead i was entering the whole number in the first iteration.īut still a lot of thanks for the replies. ("The number is not a palindrome ") Įdit: Guys the problem is actually solved because i was doing a blunder mistake i.e. I have marked the code where i think the problem lies but feel free to suggest anything.!!!! And i would like to know if that is even possible? And if it is possible then what am i doing wrong. Learn to check if a given string is palindrome string with simple java programs using stack, queue or simple loops. I wont code it up, but Ill ive pseudocode that you could try to use. You can use charAt and a loop that processes half the string and the other half at the same time. Introduction In this article, were going to see how we can check whether a given String is a palindrome using Java. I am writing a program that would help me find whether the number entered is a palindrome or not but i am trying it using arrays. A better way than recursion would be an iterative process.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |