/* Naomi Tilsen
CS 111A
Programming Lab 5 (programmingLab4.java)
Apply for extra credit.
"Palindrome Detector" is a program to take a string or sentence input from the
user on the console, and check it to see if it is a palindrome. The program uses
arrays and two user-defined methods to accomplish the task. This program will not
recognize numbers as part of a valid palindrome.
*/
import java.util.Scanner;
class ProgrammingLab5
{
// class level variables: best to avoid them, but here are two:
static int countLettersArray = 0; // the number of letters in array. Set by "removesNonLetters"
static String getString; // the string input by the user. Turned into an array,
// but saved as string for output.
public static void main(String[] args)
{
int numLoops;
boolean lettersMatch = false;
char[] arrayIn = getArray();
// call function getArray to get user input and save returned array as arrayIn
char[] lettersArray = removesNonLetters(arrayIn);
// call function removesNonLetters to make sure that all
// elements of the array are lower case letters
if (countLettersArray % 2 == 1)
// if there are an odd number of letters
numLoops = (countLettersArray - 1)/2;
// initialize the maximum number of loops to reflect the odd routine
else
numLoops = countLettersArray/2;
// initialize the maximum number of loops to reflect the even routine
for ( int counter = 0; counter < numLoops; counter ++)
{
if ( lettersArray[counter] == lettersArray[countLettersArray - (counter +1)] )
// compare the first element of the array with the last, etc.
{
lettersMatch = true;
}
else
{
lettersMatch = false;
break;
}
} // end for loop to check for matching letters
if (lettersMatch)
System.out.println ("The word/sentence you entered, '" +getString +"' is a palindrome.");
else
System.out.println ("The word/sentence you entered, '" +getString +"' is not a palindrome.");
} // end method main
/* User-defined method getArray uses Scanner to take a string from the
user via the console, and saves each entry as an element of an array.
The array is then returned to the calling method as arrayIn.
*/
static char[] getArray ( )
{
Scanner keyIn = new Scanner (System.in);
System.out.println( "Welcome to the Palindrome Detector. Please enter a palindrome." );
System.out.print( "You may enter a word or a sentence - numbers and punctuation don't count. \n> ");
getString = keyIn.nextLine(); // save keyIn input line as getString
char getElements[] = getString.toCharArray();
// save getString as as new char array getElements
return getElements;
} // end method getArray
/* User-defind method removesNonLetters accepts an array of String elements
checks to see which elements are not letters, and saves the letters only
to a second array without spaces or non-letter characters. The second array
is then returned to the calling method as lettersArray.
*/
static char[] removesNonLetters ( char arrayIn[] )
{
countLettersArray = 0;
char[] lettersArray = new char [arrayIn.length];
// assume the length of the new array will be
// equal to or less than the length of arrayIn
for ( int countArrayIn = 0; countArrayIn <= arrayIn.length - 1; countArrayIn ++)
{
if ( Character.isLetter(arrayIn[countArrayIn]) ) // if element in arrayIn is a letter
{
lettersArray[countLettersArray] = Character.toLowerCase(arrayIn[countArrayIn]);
// convert that element to lower case and copy it
// into the next index for lettersArray
countLettersArray ++; // use this number to count the number of loops in main method
}
} // end for loop to convert arrayIn to lettersArray
return lettersArray;
} // end method removesNonLetters
} // end class ProgrammingLab5
/********* Begin output for ProgrammingLab4.java *********
C:\j2sdk1.4.2_03\docs>javac ProgrammingLab4.java
C:\j2sdk1.4.2_03\docs>java ProgrammingLab4
Welcome to the Palindrome Detector. Please enter a palindrome.
You may enter a word or a sentence - numbers and punctuation don't count.
> radar
The word/sentence you entered, 'radar' is a palindrome.
C:\j2sdk1.4.2_03\docs>java ProgrammingLab4
Welcome to the Palindrome Detector. Please enter a palindrome.
You may enter a word or a sentence - numbers and punctuation don't count.
> A man, a plan: a canal - Panama!
The word/sentence you entered, 'A man, a plan: a canal - Panama!' is a palindrome.
C:\j2sdk1.4.2_03\docs>java ProgrammingLab4
Welcome to the Palindrome Detector. Please enter a palindrome.
You may enter a word or a sentence - numbers and punctuation don't count.
> abcdeedcba
The word/sentence you entered, 'abcdeedcba' is a palindrome.
C:\j2sdk1.4.2_03\docs>java ProgrammingLab4
Welcome to the Palindrome Detector. Please enter a palindrome.
You may enter a word or a sentence - numbers and punctuation don't count.
> abcdecba
The word/sentence you entered, 'abcdecba' is not a palindrome.
C:\j2sdk1.4.2_03\docs>java ProgrammingLab4
Welcome to the Palindrome Detector. Please enter a palindrome.
You may enter a word or a sentence - numbers and punctuation don't count.
> Good Dog!
The word/sentence you entered, 'Good Dog!' is not a palindrome.
C:\j2sdk1.4.2_03\docs>
********* End output for ProgrammingLab4.java *********/
syntax highlighted by Code2HTML, v. 0.9