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
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); } }
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...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
(Using Java) create a class that can identify a palindrome. A palindrome is defined as -...
(Using Java) create a class that can identify a palindrome. A palindrome is defined as - A string of characters that reads the same from left to right as its does from right to left - Example: Anna, Civic, Kayak, Level, Madam - To recognize a palindrome, a queue can be used in conjunction with a stack o A stack can be used to reverse the order of occurrences o A queue can be used to preserve the order of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT