Question

In: Computer Science

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

Solutions

Expert Solution

1)

// java program for checking string polindrome(Case sensitive)
import java.util.*;
import java.util. Scanner;
class Main
{
public static void main(String args[])
{
String orig, reverse = "";
  
Scanner scann = new Scanner(System.in);
// taking string from user
System.out.println("Enter a string to check : ");
orig = scann.nextLine();
// finding length of string
int length = orig.length();
if(length==5)
{
for (int i =length - 1; i >= 0; i--)
// reversing original string
reverse = reverse + orig.charAt(i);

// equals() wil check whether both are equal or not
if (orig. equals(reverse)) // cheking equal or not
System.out.print("It is a palindrome");
else
// if both are not equal then it is not a polindrome
System.out.print("It isn't a palindrome.");
}
else
{
// asking to enter length 5 string only
System.out.print("Enter String of length 5 only : ");
}
}
}

output

2)

// java program for checking string polindrome(Case sensitive)
import java.util.*;
import java.util. Scanner;
class Main
{
public static void main(String args[])
{
String orig, reverse = "";
  
Scanner scann = new Scanner(System.in);
// taking string from user
System.out.println("Enter a string to check : ");
orig = scann.nextLine();
// finding length of string
int length = orig.length();
if(length==5)
{
for (int i =length - 1; i >= 0; i--)
// reversing original string
reverse = reverse + orig.charAt(i);

// toLowerCase() will change all smalls to capital letters.
orig=orig.replaceAll("[^A-Za-z]", ""). toUpperCase();
reverse=reverse.replaceAll("[^A-Za-z]", "").toUpperCase();
// equals() wil check whether both are equal or not
if (orig. equals(reverse)) // cheking equal or not
System.out.print("It is a palindrome");
else
// if both are not equal then it is not a polindrome
System.out.print("It isn't a palindrome.");
}
else
{
// asking to enter length 5 string only
System.out.print("Enter String of length 5 only : ");
}
}
}

output


Related Solutions

Meant to be written in Java JDK 14.0 4. Define 2 String variables str1 and str2,...
Meant to be written in Java JDK 14.0 4. Define 2 String variables str1 and str2, get input from console and assign input to str1 and str2. (a) Display the length of str1 and str2 (b) Display the first and last characters in str1 and str2 (c) Compare str1 and str2 to check if they are equal or not: (1) The comparison is case sensitive (2) The comparison is case insensitive (d) Compare str1 with str2 to check which string...
Meant to be written in Java JDK 14.0 Write a program to display the conversion table...
Meant to be written in Java JDK 14.0 Write a program to display the conversion table from meter to feet using formatted output (printf()): 1 meter = 3.28084 feet; 1 foot = 12 inch When display the number, round the number to 2 decimal places. Set the number in the 1st the 2nd columns to the left align, set the 3rd column to the right align: meter(s) feet inch(es) 1 3.28 37.37 2 x.xx xx.xx 3 x.xx xxx.xx
Meant to be written in Java JDK 14.0 In New York state, the vehicle plate number...
Meant to be written in Java JDK 14.0 In New York state, the vehicle plate number has the following format: xxx-yyyy, where x is a letter and y is a digit. Write a program to enter a string, verify if it is a valid plate number or not
Meant to be written in Java JDK 14.0 1. Define 2 double variables. (a) Use method(s)...
Meant to be written in Java JDK 14.0 1. Define 2 double variables. (a) Use method(s) define in Math class to check if these 2 numbers are equal or not. Display result (b) Write code (not to use Math method) check if these 2 numbers are equal or not. Display result
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...
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...
Java Palindrome Use StringBuilder concept to create a palindrome checker.   Method should be called pCheck. Must...
Java Palindrome Use StringBuilder concept to create a palindrome checker.   Method should be called pCheck. Must use var-arg concept.   Class name for this program is Palindrome.   Must pass this test:: public class PalindromeTest { public static void main(String[] args) { Palindrome palindrome = new Palindrome(); boolean answer = palindrome.pCheck("racecar", "Bell", "elle", "bunny"); System.out.println(“These are palindromes: " + answer); } }
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...
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) Declare a String variable called myString0 and initialize it to "Java". B) Declare a String...
A) Declare a String variable called myString0 and initialize it to "Java". B) Declare a String variable called myString1 and initialize it to "Programming". C) Declare a String variable called myString2 and initialize it to "Language". D) Print the concatenation of myString0 + " is a " + myString1 + " " + myString2 + ".". E) Print the sum of the lengths of myString1 and myString2 (must call the length() method). F) Print the 2nd, 4th, and 7th character...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT