-
November 28th, 2008, 11:21 AM
#1
[help me plz] palindrome program
so is there anyway to make this program more simplier. its basically a palindrome program. a Palindrome is a word that reads a word same from left to right as does from right to left.
Example palindrome words - CIVIC , DAD , RADAR
as u can see these words can be spelt frontwards and backwards
so heres the program can it be made more simplier
class Palindrome
public static void main(String[] args){
String str1 = "COMPUTER", str2 = "RADAR";
System.out.println("Palindrome detection");
System.out.println(str1 + " "
+ isPalindrome(str1));
System.out.println(str2 + " "
+ isPalindrome(str2));
}
static boolean isPalindrome(String s) {
int left = 0;
int right = s.length() - 1;
while (left < right) {
if (s.charAt(left) != s.charAt(right))
return false;
left++;
right--;
}
return true;
}
}
-
November 28th, 2008, 12:56 PM
#2
Re: [help me plz] palindrome program
In what way do you want it to be made simpler and what do you mean by simpler - for example do you mean less lines of code, easier to understand or a different algorithm.
-
November 28th, 2008, 02:33 PM
#3
Re: [help me plz] palindrome program
I don't think I'll be giving too much away by saying that the StringBuilder class has a reverse() method, and you can compare two Strings using the equals(..) method. Simpler, neater, more efficient - job done.
Simplicity is the ultimate sophistication...
L. Da Vinci
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
November 29th, 2008, 10:41 PM
#4
Re: [help me plz] palindrome program
The last time I wrote this program, I used a recursive function to compare the first and last characters; if they are the same, increment/decrement your pointers(first and last using charAt(), possibly) and _call the function again_.
Let me know how that works.
P.S.
To use iteration, a for loop will do fine.
i can be your pointer to "first"
and j (inside the forloop can be decremented to act as the pointer to "last")
for example:
for (int i = 0; i < str.length(); i++)
{
if ( str.charAt(i) == str.charAt(j) ) <--- same goes for the recursion (then do it again)
//such and forth
j--;
}
Last edited by TheStudent; November 29th, 2008 at 11:45 PM.
Reason: Clarification.
-
December 1st, 2008, 05:41 AM
#5
Re: [help me plz] palindrome program
Why reinvent the wheel? It's a one-liner - the OP asked to make it simpler, not more complicated...
Code:
if (word.equals(new StringBuilder(word).reverse().toString())) {
System.out.println(word + " is a palindrome");
}
All truths are easy to understand once they are discovered; the point is to discover them...
G. Galilie
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
December 3rd, 2008, 06:51 AM
#6
Re: [help me plz] palindrome program
I just had the same assignment but we had to handle whitespaces and the method had to take a line for testing.
My solution is probably not the best but it works well:
/***** recursive method *****/
public static boolean testPalindrome(String string){
int begin = 0; // beginning marker
int end = string.length()-1; // ending marker
char char1 = string.charAt(begin); // character one
char char2 = string.charAt(end); // character two
/*** eliminate spaces ***/
if(char1 == ' ' && string.length() > 3){
begin++;
return testPalindrome(string.substring(begin, end));
}
else if(char2 == ' ' && string.length() > 3){
end--;
return testPalindrome(string.substring(begin, end));
}
/*** handle false ***/
else if(char1 != char2){
return false;
}
/*** handle base case aab-bcc ***/
else if((Character.toLowerCase(char1) == Character.toLowerCase(char2)) &&
(end + 1) == 2){
return true;
}
/*** handle base case aa-b-cc ***/
else if((Character.toLowerCase(char1) == Character.toLowerCase(char2)) &&
(end + 1) == 3){
return true;
}
/*** make recursive call ***/
return testPalindrome(string.substring(++begin, end));
} // end method
Jason Clinkscales
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|