35.5k views
5 votes
Write a method named isPalindrome that accepts a string parameter and returns true if that string is a palindrome, or false if it is not a palindrome. For this problem, a palindrome is defined as a string that contains exactly the same sequence of characters forwards as backwards, case-insensitively. For example, "madam" or "racecar" are palindromes, so the call of isPalindrome("racecar") would return true. Spaces, punctuation, and any other characters should be treated the same as letters; so a multi-word string such as "dog god" could be a palindrome, as could a gibberish string such as "123 $$ 321". The empty string and all one-character strings are palindromes by our definition. Your code should ignore case, so a string like "Madam" or "RACEcar" would also count as palindromes.

User Rootkea
by
7.3k points

1 Answer

0 votes

Answer:

The programming language is not stated;

However, I'll answer this question using Java Programming Language;

The method is as follows;

public static void isPalindrome(String userinput)

{

String reverse = "";

int lent = userinput.length();

int j = lent - 1;

while(j >= 0)

{

reverse = reverse + userinput.charAt(j);

j--;

}

if(reverse.equalsIgnoreCase(userinput))

{

System.out.println("True");

}

else

{

System.out.println("False");

}

}

Step-by-step explanation:

This line defines the method isPalindrome with a string parameter userinput

public static void isPalindrome(String userinput) {

This line initializes a string variable

String reverse = "";

This line gets the length of the userinput

int len = userinput.length();

The following while-loop gets the reverse of userinput and saved the reversed string in reverse

int j = lent - 1;

while(j >= 0)

{

reverse = reverse + userinput.charAt(j);

j--;

}

The following if statement checks if userinput and reverse are equal (cases are ignored)

if(reverse.equalsIgnoreCase(userinput))

{

System.out.println("True"); This line is executed if the if condition is true

}

else

{

System.out.println("False"); This line is executed if otherwise

}

} The method ends here

User Jagadesh Seeram
by
7.4k points