Results 1 to 2 of 2
Thread: simple code
- 08-07-2007, 06:45 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
simple code
The following function determine if its parameter is a string which reads exactly the same forward or backward. There is an error in the code.
help pleaseJava Code:boolean isPalindrome(String s) { int i=0, j = s.length( ) -1; while ( i !=j && s.charAt(i)==s.charAt(j)) { i++; j--; } return (i==j); }
- 08-07-2007, 06:49 PM #2
Member
- Join Date
- Jul 2007
- Posts
- 43
- Rep Power
- 0
You code will work only if the string lenght is odd. Wont work if it is even
good luckJava Code:boolean isPalindrome(String s) { int i=0, j = s.length( ) -1; while ( i !=j && s.charAt(i)==s.charAt(j)) { i++; j--; } return (i==j); } try these changes it will work boolean isPalindrome(String s) { int i=0, j = s.length( ) -1; while ( i !=j && s.charAt(i)==s.charAt(j)) { // change the condition here from i!=j to i<=j i++; j--; } return (i==j); //Change the condition here from i==j to i>j }
Similar Threads
-
a simple code (cldc or midp) to write a text file ouside of the application package
By sina in forum CLDC and MIDPReplies: 3Last Post: 12-12-2008, 12:12 PM -
Peculiarty in code of simple program...
By Kreuz14 in forum New To JavaReplies: 4Last Post: 01-23-2008, 03:27 AM -
simple problem - code wont compile
By dirtycash in forum New To JavaReplies: 1Last Post: 11-20-2007, 05:49 PM -
problem with a simple java code
By boy22 in forum New To JavaReplies: 2Last Post: 08-03-2007, 02:46 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks