user2045470
user2045470

Reputation: 59

Checking to see if a string is a palindrome or not

I am very close to finishing my one practice problem that deals with a palindrome and a string parameter and I am stuck with the main method to call the method. Every time I compile my code it compiles, but then when I go to input data, it keeps on running and does not give me a result. Can anyone aid in me in what I need to do to get it to return the result? The problem just asks to create a method that checks if it is a palindrome, my main method to test it is what is giving me trouble.

This is my code:

import java.util.*;
public class TestisPalindrome
{
  public static boolean isPalindrome(String str) {
    int left = 0;
    int right = str.length() -1;

    while(left < right) {
      if(str.charAt(left) != str.charAt(right)) {
        return false;
      }
    }
    left ++;
    right --;

    return true;
  } 

  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a string to see if it is a palindrome or not: ");
    String st1 = scan.nextLine();
    System.out.println(isPalindrome(st1));
  }
}

Upvotes: 2

Views: 5024

Answers (5)

PShah
PShah

Reputation: 11

You can also solve it like this:

public static boolean isPalindrome (String str){
    String convertedStr = "";

    for (int i = 0; i <str.length(); i++){
        if (Character.isLetterOrDigit(str.charAt(i)))
            convertedStr += Character.toLowerCase(str.charAt(i));
    }
    if (convertedStr.equals(reverseString(convertedStr)))
        return true;
    else
        return false;    
} //End of isPalindrome

Upvotes: 1

mlchen850622
mlchen850622

Reputation: 668

Here is the code I used to determine whether a string is Palindrome String or not:

private static boolean isPalindromeString(String str){
    if (str == null) 
        return false;
    int len  = str.length();
    for (int i=0; i<len/2 ; i++){
        if (str.charAt(i) != str.charAt(len - i - 1)){
            return false;
            }
    }       
    return true;
}

I hope this can help you.

Upvotes: 0

Nathaniel Ford
Nathaniel Ford

Reputation: 21220

You are overthinking this. Look at StringBuffer:

StringBuffer input = new StringBuffer(str);
return str.equals(input.reverse()).toString);

Please note that there is a performance impact of your implementation:

while(left < right) { //multiply inner operations by n/2
  if(str.charAt(left) != str.charAt(right)) { //three operations
    return false;
  }
  //This need to be inside your while loop
  left ++; //one operation
  right --; //one operation
}

This leads to an O(n) = (n * 5) / 2. On the other hand, if you simply reverse a string, it's only O(n) = n in the worst case. This is not a significant impact, but can add up depending on how you're accessing this.

Upvotes: 1

Steve
Steve

Reputation: 133

You've created an infinite loop. You have a while loop, but never change the conditions.

while(left < right)
{
    if(str.charAt(left) != str.charAt(right))
    {
        return false;
    }
}

Assuming left < right when you start, this will never change.

You have lines to increment left and right, but your code will never reach those lines, since it never gets out of the while loop.

Upvotes: 2

Ankit
Ankit

Reputation: 6622

right & left increment should be in while loop

while(left < right)
     {
        if(str.charAt(left) != str.charAt(right))
        {
            return false;
        }
        left ++;
        right --;
    }

Upvotes: 4

Related Questions