View Single Post
Old 04-24-07, 04:53 PM   #3
Absolution
AbsoUI
 
Absolution's Avatar
 
Join Date: Dec 2004
Posts: 933
Default Re: Java Help: Palindrome program

this is quite a bit smaller
Code:
// 
// CS161L Wed 3:00 PM
// 9:00 Lecture Mon,Wed,Fri
// Lab9 - lab10.java
import java.util.*;

public class lab10
{
	public static boolean palindrone(String s, int index)
	{
		if(index == s.length())		// drop out of the loop, string is at the end
			return true;
		char first,last;
		first = s.charAt(index);
		last = s.charAt(s.length()-index-1);
		//System.out.println(first + " " + last);
		if(first == last)
			return palindrone(s,++index);
		else
			return false;
	}
	public static void main(String args[])
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a word, 'quit' to exit: ");
		String in = input.nextLine();
		while(!in.equals("quit"))
		{
			if(palindrone(in,0))
				System.out.println("This is a palindrone");
			else
				System.out.println("This is not a palindrone");
			System.out.print("Enter another word: ");
			in = input.nextLine();
		}
	}
}
Absolution is offline   Reply With Quote