|
-
November 16th, 2003, 06:56 AM
#4
Member
If you want one in Java, I'll be glad to edit my normal palindrome tester which I did for an AP Java class, now it tests entire sentences, here's the source below:
Code:
/*
Dave T
Date: September 30, 2003
Page: 184 Problem 3.12
The Quote of the Day:
"Eat right, don't smoke, die anyway."
*/
import cs1.Keyboard;
import java.util.Random;
import java.util.StringTokenizer;
import java.text.NumberFormat;
public class PalindromeTester
{
public static void main (String[] args)
{
String str, another = "y", lower;
int left, right;
while (another.equalsIgnoreCase("y"))
{
System.out.println ("Enter a potential palindrome:");
str = Keyboard.readString();
lower = str.toLowerCase();
left = 0;
right = str.length() - 1;
while (lower.charAt(left) == ' ' || lower.charAt(left) == '.' || lower.charAt(left) == ',' || lower.charAt(left) == '?')
{left++;}
while (lower.charAt(right) == ' ' || lower.charAt(right) == '.' || lower.charAt(right) == ',' || lower.charAt(right) == '?')
{right--;}
while (lower.charAt(left) == lower.charAt(right) && left <= right)
{
left++;
right--;
while (lower.charAt(left) == ' ' || lower.charAt(left) == '.' || lower.charAt(left) == ',' || lower.charAt(left) == '?')
{left++;}
while (lower.charAt(right) == ' ' || lower.charAt(right) == '.' || lower.charAt(right) == ',' || lower.charAt(right) == '?')
{right--;}
}
System.out.println();
if (left < right)
System.out.println("That string is NOT a palindrome.");
else
System.out.println("That string IS a palindrome.");
System.out.println();
System.out.print("Test another palindrome (y/n)? ");
another = Keyboard.readString();
}
}
}
I know I have a lot of useless classes being imported, but since I use an outline for every program thats been premade, I really don't delete any...
If it has to be in C++, drop me a line, I still remember of some of that stuff...
[pong][shadow]Why won\'t anyone give me greenies???[/shadow] [/pong]
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
|
|