Hi everybody. I appreciate any help given, and appreciate your time. I'm currently doing a program that tests if the input is a palindrome or not. I have already done the reversing part of the program. But was wondering how I could possibly check to see if the input word is a palindrome. Here's what I have done so far, thanks a lot. Any advice is appreciated.

private void reversePalindrome()
{
	String palindrome="";
        char[] array=palindrome.toCharArray();
        char[] carol=new char[palindrome.length()];
        for(int i=array.length-1, j=0; i>=0; i--, j++) 
        {
            carol[j]=array[i];
        }
       	
       	
        System.out.println(carol);
}

Since I have to consider punctuation too, I have thought of converting the input into a string tokenizer. Would this help to check for palindronism?

Well it would be easy to check if a word is a palindrome using recursion. I will show you how to use recursion to find a palindrome, since you are new to this and since recursion is not an easy concept to grapse. But an easy way to design your program is to think about what you have to check for in a word to determine if it is a palindrome and what will be the simpliest way to do it. One way is to check the first and the last character of the string to see if they are equal, create a new string without the first and the last character and do the same process until the string length is equal to 1 or 0, here is an recursive example:

public static boolean palindrome(String word)
{
          boolean isPalindrome = true;
	
          //base case which stops the recursing	
          if(word.length()== 1 || word.length() == 0 )
          {
			
          }
          else
          {  
                //checks first character and last character of string
                if(word.charAt(0) == word.charAt(word.length()-1))
                 {
                      //recursive call to palindrome with new string
                      isPalindrome = palindrome(word.substring(0+1, word.length()-1));

                  }
                  else isPalindrome = false;			
           }
		
           return isPalindrome;	
		
}

By the way why did you reverse the palindrome?

private void reversePalindrome()
{
	String palindrome="";
        char[] array=palindrome.toCharArray();
        char[] carol=new char[palindrome.length()];
        for(int i=array.length-1, j=0; i>=0; i--, j++) 
        {
            carol[j]=array[i];
        }
       	
       	
        System.out.println(carol);
}

you almost have everything you need. just use this array, and check whether or not
array and array[palindrome.length() - i] are the same, while i <= palindrome.length() / 2

Why so difficult? You can do it in a single line of code...

return new StringBuilder(input).equals(new StringBuilder(input).reverse());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.