Created
October 5, 2015 02:51
-
-
Save anonymous/cf1d66899190e10fcbe6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Palindrome_class | |
{ | |
// instance variable | |
char[] characterArray; | |
//constructor | |
//@param data is a string of characters | |
public Palindrome_class(String data) | |
{ | |
characterArray = data.toUpperCase().toCharArray(); | |
} | |
//@return true if the word is a palindrome, otherwise returns false. | |
public boolean isPalindrome(char[] a, int used) | |
{ | |
int i = 0, j = used - 1; | |
while (i < j) | |
{ | |
if(characterArray[i] == characterArray[j]) | |
{ | |
i++; | |
j--; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class PalindromeTest | |
{ | |
public static void main(String[] args) | |
{ | |
int used = 0; | |
char[] chars = new char[80]; | |
Scanner inputWord = new Scanner(System.in); | |
Scanner reply = new Scanner(System.in); | |
System.out.println("Enter a string characters, terminated by a period."); | |
String data; | |
String cq; | |
Palindrome_class word; | |
do | |
{ | |
//input word from user. | |
data = inputWord.nextLine(); | |
word = new Palindrome_class(data); | |
//check for palindrome. | |
if(word.isPalindrome(chars, used)) | |
System.out.println(data + " is a palindrome."); | |
else | |
System.out.println(data + " is not a palindrome."); | |
//request to continue or quit. | |
System.out.println("Continue or Quit?"); | |
cq = reply.nextLine(); | |
} | |
while (cq.equalsIgnoreCase("continue")); | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment