Urgent! Need help on Palindrome problem - toCharArray

Started this coding and not sure where to go from here.One of the requirement is the isPal() must take in a char[], int indexNum - that's one of the requirement.

***************************************************************

import java.io.*;

import java.util.StringTokenizer;

public class Palindrome {

private static char[] palindrome;

int sizeOfArray;

private static String text, inText;

public Palindrome()

{

sizeOfArray = 30;

//create an empty array

palindrome = new char[sizeOfArray];

}

public String setPalindrome(String in)

{

StringTokenizer st = new StringTokenizer(in, ".");

text = in;

inText = st.nextToken();

return inText;

}

/*****************************************************************

* a char array containing the characters of the *

* input string, and an integer, number, defining the number of *

* characters in the string *

******************************************************************/

private static boolean isPal(char[] pal, int number)

{

palindrome = pal;

int index = number;

//validate if the character is less than 2 - means that there is only 1 character

if (pal.length< 2)

return true;

//validate if first character == the last position value in the array

//if (pal[0] == pal[pal.length-1])

//return isPal(pal.substring(1, pal.length-1));

return false;

}

public static void main(String[] args)throws IOException

{

String input;

boolean unequal = false;

Palindrome pal = new Palindrome();

//user input

BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));

System.out.print("Please enter a potential palindrome(enter a period at the end): " );

System.out.flush();

input = stdin.readLine().toLowerCase();

pal.setPalindrome(input);

System.out.println(inText);

//Add input String to a CharArray() - palindrome

palindrome = inText.toCharArray();

int lengthToCheck = palindrome.length/2;

for(int i =0; i<lengthToCheck; i++)

{

if (palindrome[ i ] != palindrome[palindrome.length - (i +1)])

{

unequal = true;

break;

}

//print out the palindrome char[] values.

System.out.println(palindrome);

}

}//end main()

}//end class

>

[2537 byte] By [slailui0402] at [2007-9-26 12:22:04]
# 1

//Add input String to a CharArray() - palindrome

palindrome = inText.toCharArray();

int lengthToCheck = palindrome.length/2;

for(int i =0; i<lengthToCheck; i++)

{

if (palindrome[ i ] != palindrome[palindrome.length - (i +1)])

{

unequal = true;

break;

}

1.move the above code of yours to isPal and return 'unequal'

2.Call isPal(palindrome, number) in its place.

if( isPal(palindrome,number) ){

System.out.println( inText + " is a palindrome.");

}else{

System.out.println( inText + " is not a palindrome.");

}

3. If the length is 5 you need to loop floor(5/2) = 2

if length is 4 you need to loop floor(4/2) = 2

if length is 9 you need to loop floor(9/2) = 4

because if the number of characters are odd you don't need to compare the middle character any way, so use the floor method of class Math. this method can be called directly on class since it is a statci method.

so do

int lengthToCheck = (int)Math.floor( palindrome.length/2 );

instead of

int lengthToCheck = palindrome.length/2;

>

bharatchhajer at 2007-7-2 3:00:47 > top of Java-index,Archived Forums,Java Programming...
# 2

import java.io.*;

public class test46

{

public static void main(String[] args) throws IOException

{

System.out.print("Please enter the string: ");

BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));

String str = stdin.readLine().toLowerCase();

if (isPalindrome(str))

{

System.out.println("input string is a palindrome");

}

else

{

System.out.println("input string is not a palindrome");

}

}

public static boolean isPalindrome(String str)

{

boolean retVal = true;

int length = str.length();

if (length < 2)

{

retVal = true;

}

else

{

for(int i = 0; i < length -1; i++)

{

if (str.charAt(i) == str.charAt((length -1) - i)) continue;

else

{

retVal = false;

break;

}

}

}

return retVal;

}

}

abhishek_srivastava at 2007-7-2 3:00:47 > top of Java-index,Archived Forums,Java Programming...
# 3
for the for loop please usefor(int i = 0; i < (length -1)/2; i++){.....} instead of i < (length -1)
abhishek_srivastava at 2007-7-2 3:00:47 > top of Java-index,Archived Forums,Java Programming...
# 4

Thanks for the info. I actually got it working this morning. But now I am dealing with another problem. My program below is working fine now. But there is this one part I am not sure how to solve. when I receive the string from the buffer reader and before I put that into the toCharArray[],,,I think...I need to disregard blanks when deciding if a string is a palindrome and consider upper and lower case version of the same letter to be equal. I Began to put a piece of code using String Buffer....not sure how to go about further.(see below)

public static void main(String[] args)throws IOException

{

String input;

Palindrome pal = new Palindrome();

//user input

BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));

System.out.print("Please enter a potential palindrome(enter a period at the end to indicate termination): " );

System.out.flush();

input = stdin.readLine().toLowerCase();

StringBuffer sbuf = new StringBuffer();

for(int j=0; j<input.length(); j++)

{

if (input.charAt(j))

sbuf.append(input.charAt(j));

}

pal.getPalindrome(input);

System.out.println("Input text: " + inText + "\n");

//find out the length of the string

palindrome = inText.toCharArray();

length = palindrome.length;

System.out.println("Lenght of potential palindrome: " + length + "\n");

//Validate input string and determine whether this is a palindrome

pal.isPal(palindrome, length);

if(unequal == false)

System.out.println("It is a palindrome. ");

else

System.out.println("It is not a palindrome.");

}//end main()

>

slailui0402 at 2007-7-2 3:00:47 > top of Java-index,Archived Forums,Java Programming...
# 5
use trim() method of String to remove left right spaces.use toLowerCase() method to convert all characters to lower case( in effect you can avoid problem of lower and upper case ).
bharatchhajer at 2007-7-2 3:00:47 > top of Java-index,Archived Forums,Java Programming...
# 6
I understand that you could use the trim(). But what if a string like "warts n straw" has spaces in between that, the trim() won't work, right?
slailui0402 at 2007-7-2 3:00:47 > top of Java-index,Archived Forums,Java Programming...
# 7
trim() only removes spaces from the right and left extremes not from the middle of a string. so it isn't a problem.
bharatchhajer at 2007-7-2 3:00:47 > top of Java-index,Archived Forums,Java Programming...