In: Computer Science
PLEASE DO IN JAVA
4.15 Palindrome
A palindrome is a string that is the same spelled forward as it is spelled backward. So, "abcba" is a palindrome, as are "eye" and "madam". This program takes as input from the user, a string and outputs whether the string is a palindrome.
(1) Modify the given program to use a loop to output the string one character at a time. (1 pt)
Example output for word = radar:
Word Entered: radar r a d a r
(2) Now modify the program to use a loop to output the string in reverse order one character at a time. (2 pt)
Example output for word = read:
Word Entered: read r e a d Word Reversed: d a e r
(3) Finally, modify the program to check character-by-character the string given by the user to see whether or not the string is a palindrome, and then prints either "Yes" or "No". For example, if the input string is "abba" or "rotator", the output would be "Yes". If the input string is "cat" or "garbage", the output would be "No". You may ignore punctuation, in that the test strings used for this program do not contain any. You may also assume all lowercase letters will be used in the test strings.
Note: The strings may be of odd or even length, as in "cat", "dad", "racecar", or "hannah". (8 pt)
Example output for word = radar:
Word Entered: radar r a d a r Word Reversed: r a d a r Yes
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
/* Your Code Here */
return;
}
THANK YOU SO MUCH
ANSWER:-
QUESTION :-
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String str, rev = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
str = sc.nextLine();
int length = str.length();
for ( int i = length - 1; i >= 0; i-- )
rev = rev + str.charAt(i);
if (str.equals(rev))
System.out.println(str+" is a palindrome");
else
System.out.println(str+" is not a palindrome");
}
}
OUTPUT:-
QUESTION (1):-
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String str, rev = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
str = sc.nextLine();
for(int i=0;i<str.length();i++)
System.out.println(str.charAt(i));
}
}
OUTPUT:-
QUESTION (2):-
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String str, rev = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: read");
str = sc.nextLine();
for(int i=str.length()-1;i>=0;i--)
System.out.println(str.charAt(i));
}
}
OUTPUT:-
QUESTION (3):-
import java.util.Scanner;
class Palindrome {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int flag=0;
String str, rev = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
str = sc.nextLine();
for(int i=0;i<str.length();i++)
System.out.println(str.charAt(i));
int length = str.length();
for (int i = 0; i < (length/2); ++i) {
if (str.charAt(i) != str.charAt(length - i - 1))
flag=1;
}
System.out.println("Word reverse : ");
for(int i=str.length()-1;i>=0;i--)
System.out.println(str.charAt(i));
if (flag==1)
System.out.println("No");
else
System.out.println("Yes");
}
}
OUTPUT:-
Enter a string: radar r a d a r Word reverse : r a d a r Yes