DEV Community

Sadiul Hakim
Sadiul Hakim

Posted on

Algorithm part 4: Check if a string is palindrome or not

Hey, Guys. Today i am going to show you How you can check if a string is palindrome.

what is palindrome?

Suppose you have a string mom. Now if you reverse this string you get mom again. So this is a palindrome string.

Solution

public boolean isPalindrome(String str){
        char[] arr=str.toCharArray();

        int i=0;
        int j=arr.length-1;

        while(i<j){
            char temp=arr[i];
            arr[i]=arr[j];
            arr[j]=temp;

            i++;
            j--;
        }

        return str.equals(new String(arr));
    }
Enter fullscreen mode Exit fullscreen mode

Hope this helps you. Thank you ❤.

Top comments (0)