Question

In: Computer Science

PLEASE DO IN JAVA 4.15 Palindrome A palindrome is a string that is the same spelled...

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

Solutions

Expert Solution

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

Related Solutions

IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards....
IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty...
C++: A palindrome is a string that is the same backward as it is forward. For...
C++: A palindrome is a string that is the same backward as it is forward. For example, “tot” and “otto” are rather short palindromes. Write a program that lets a user enter a string and that passes to a bool function a reference to the string. The function should return true if the string is a palindrome and false otherwise. When you do the judgment, capitalization, spaces, and punctuation should be neglected, that is, “Madam, I’m Adam” should test as...
IN JAVA PLEASE The Palindrome class will have a single constructor that accepts a String argument...
IN JAVA PLEASE The Palindrome class will have a single constructor that accepts a String argument and checks to see if the String is a palindrome. If it is a palindrome it will store the palindrome and the original String as state values. If is not a palindrome it will throw a “NotPalindromeError” and not finish the creation of the new Palindrome object. The constructor will use a single stack to evaluate if the String is a palindrome. You must...
In C++: This is the problem: [(1)] A palindrome is a string that reads the same...
In C++: This is the problem: [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q...
A palindrome is a string of characters (a word, phrase, or sentence) that is the same...
A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward – assuming that you ignore spaces, punctuation and case. For example, Race car is a palindrome. So is A man, a plan, a canal: Panama. 1. Describe how you could use a stack to test whether a string is a palindrome. 2. Describe how you could use a queue to test whether a string is...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as...
A palindrome is a string that reads the same forward and backward, i.e., the letters are...
A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from right to left or from left to right.      Examples: radar à is a palindrome Able was I ere I saw Elba à is a palindrome good à not a palindrome Write a java program to read a line of text and tell if the line is a palindrome. Use a stack to read each non-blank character...
JAVA Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward...
JAVA Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward or backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a boolean method that users recursion to determine where a String argument is a palindrome. The method should return true if the argument reads the same forward and backward. Demonstrate the method in a program. Include the following...
Foundation of Computer Science A palindrome is a string that reads the same forward and backward....
Foundation of Computer Science A palindrome is a string that reads the same forward and backward. 1. Describe an algorithm that determines whether a string of n characters is a palindrome. 2. Write its corresponding program using your favorite programming language.
Meant to be written in Java JDK 14.0 A String object is called palindrome if it...
Meant to be written in Java JDK 14.0 A String object is called palindrome if it is same when read from the front and the end. For example, String “abcba” is palindrome, while “12322” is not. Write Java program to check a String object of 5 characters is palindrome or not: (a) The comparison is case sensitive (b) The comparison is case insensitive
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT