# String Palindrome in java: Algorithm and programs

#### What is a string palindrome in java?

String palindrome in java is the number or the string is the same after reversing it.

for example:-

1. we take a number 121 the palindrome of this number is 121, so it is a palindrome number.
2. next, we take number 123 the palindrome of this number is 321, so it is not a palindrome number.
3. now we take a string “baab” the palindrome of this string is “baab”, so it is a palindrome string.
4. now we take a string “gettingplaced” the palindrome of this string is “decalpgnitteg”, so it is not a palindrome string.

#### Algorithm

```START
Step 1 → Take a sting a
Step 2 → find the lenght of string using length() in n
Step 3 → using for loop (loop=n-1;loop>=0;loop-)
Step 4 → take an integer b
Step 5 → initilized b with b+a.charAt(loop);
Step 6 → if a.equalsIgnoreCase(b) is palindrome
Step 7 → else it is not palindrome
STOP```

#### The string is a palindrome in java using a scanner

import java.util.*;
public class Main
{
public static void main(String args[])
{
String x, y = “”;
Scanner s = new Scanner(System.in);
System.out.print(“Enter the string : “);
x = s.nextLine();
int n = x.length();
for(int loop = n – 1; loop >= 0; loop–)
{
y = y + x.charAt(loop);
}
if(x.equalsIgnoreCase(y))
{
System.out.println(“The string is a palindrome”);
}
else
{
System.out.println(“The string is not a palindrome”);
}
}
}

#### Output

Input – Enter the string: baab Output – The string is a palindrome

#### The string is a palindrome in java using a while loop

`public` `class` `palindrome { `
` `
`    ``static` `boolean` `Palindrome(String st) `
`    ``{`
`        ``int` `n = ``0``, m = str.length() - ``1``; `` `
`        ``while` `(n < m) { `` `
`            ``if` `(str.charAt(n) != str.charAt(m)) `
`                ``return` `false``; `` `
`            n``++; `
`            m``--; `
`        ``} `
` `
`        ``return` `true``; `
`    ``} `
` `
`    ``public` `static` `void` `main(String[] args) `
`    ``{ `
`        ``String st = ``"gettingplaced"``; `
` `
`        ``if` `(Palindrome(st)) `
`            ``System.out.print(``"Yes,it is palindrome"``); `
`        ``else`
`            ``System.out.print(``"No,it is not palindrome"``); `
`    ``} `
`}`
` `

#### Output

`No,it is not palindrome`

#### The string is a palindrome in java using a for loop

import java.util.*;

class Palindrome {

public static void main(String args[])

{

String b = “”;

String st = “gettingplaced”;

int n = st.length();

for ( int loop = n – 1; loop >= 0; loop– )

b = b + st.charAt(loop);

if (st.equals(b))

System.out.println(” string is a palindrome.”);

else

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

}

}

#### Output

string is not a palindrome.

##### Click here for more questions in java

Prime number in java: Algorithm and Programs