Question on User-defined methods.
WAP using a method IsPalindrome(String str) to check if the string is palindrome or not.
The function returns true if it is palindrome else will return false.
Programming language: Java.
Good luck, :)
Answers
import java.util.*;
public class PalindromeString
{
public static void main(String args [ ] )
{
Scanner scan = new Scanner(System.in);
int length;
String reverseString = '' '' ;
System.out.println(''Enter a word: '');
String inputString = scan.nextLine();
length = inputString.length();
for (int i =length-1; i >=0; i --)
{
reverseString = reverseString + inputString.charAt(i);
}
if (reverseString.equalsIgnoreCase(inputString))
System.out.println(inputString + '' is palindrome'');
else
System.out.println(inputString + '' is not palindrome'');
scan.close();
}
}
Output :-
Enter a word:
Radar
Radar is Palindrome
Palindrome :
It a number/digit which remains same even after reversing the digits/letters of it. Ex: 111 , Madam.
Solution:
The given problem is solved using Java.
import java.util.*;
public class CheckPalindrome{
public static void main(String s[]){
System.out.print("Enter a string: ");
String str=(new Scanner(System.in)).nextLine();
System.out.println(">> isPalindrome(\""+str+"\") = "+isPalindrome(str));
}
static boolean isPalindrome(String str){
return str.equalsIgnoreCase(new StringBuilder(str).reverse().toString());
}
}
Sample I/O:
Enter a string: LOL
>> isPalindrome("LOL") = true
See attachment for output.