(JAVA) A palindrome is spelled the same forwards as backwards such as: Able was
ID: 3804611 • Letter: #
Question
(JAVA)
A palindrome is spelled the same forwards as backwards such as:
Able was I ere I saw Elba
Step on no pets
Devil lived
Rats Live on no evil star
We can also break down a string for analysis into its component characters using built in character methods:
Character.isSpaceChar(ch)
Character.isLetter(ch)
Character.isDigit(ch)
Character.isUpperCase(ch)
Character.isLowerCase(ch)
Allow the user to enter a multiple word string including blanks
Create a “reversed” string and compare that to the original string. If they are identical then the original is a palindrome, spelled identically forwards and backwards. Upper and lower case do not differentiate for the palindrome comparison!
Break the original string into its component characters and display the number of:
Blank spaces
Letters
Digit symbols
Uppercase letters
Lowercase letters
Number of punctuation marks (i.e. comma, period, question marks, semicolons, colons, single and double quotations, hyphens, etc.). ASCII integer values from 37 to 47 are the punctuation marks.
Tactical hints:
Determine the number of character positions in the string (i.e. string1.length( ) )
Find an individual string or character with the string1.charAt(n)
Strings positions run from 0 to N – 1 (N is the length of the string)
Example Output:
You entered "Able was I ere I saw Elba" containing 24 characters
Backwards it is "ablE was I ere I saw elbA"
"Able was I ere I saw Elba" is a Palindrome since it is identical to "ABLE WAS I ERE I SAW ELBA"
The Original String "Able was I ere I saw Elba" contains
Text Statistics are:
24 Characters in "Able was I ere I saw Elba"
Number of blanks are 6
Number of letters are 19
Number of digits are 0
Number of UPPER Case letters are 4
Number of lower case letters are 15
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String str1 = "";
int blank = 0;
int letter = 0;
int upper = 0;
int lower = 0;
int digit = 0;
char ch;
int punctuation = 0;
for (int i = 0; i < str.length(); i++) {
ch = str.charAt(str.length() - i - 1);
str1 += ch;
if (ch >= 48 && ch <= 57) digit++;
if (ch >= 65 && ch <= 91) { upper++; letter++;}
if (ch >= 97 && ch <= 123){ lower++; letter++;}
if (ch >= 37 && ch <= 47) punctuation++;
if (ch == 32) blank++;
}
int i = 0;
for (; i < str.length(); i++) {
char ch1 = str.charAt(i);
char ch2 = str1.charAt(i);
// case insensitive comparison
if (ch1 != ch2 && str.charAt(i) != (str1.charAt(i) + 32) && str.charAt(i) + 32 != str1.charAt(i)) break;
}
System.out.println("You entered " + str + " containing " + str.length() + " charactrs");
System.out.println("Its reverse is " + str1);
if (i < str.length())
// convert string to upper case
System.out.println(str + " is not palindrome as it is not identical to " + str.toUpperCase());
else
System.out.println(str + " is palindrome as it is identical to " + str.toUpperCase());
System.out.println("The original string is "+ str);
System.out.println("Text Statistics is:");
System.out.println("Total length: " + str.length());
System.out.println("No of letters: " + letter);
System.out.println("No of upper case letters: " + lower);
System.out.println("No of lower case letters: " + upper);
System.out.println("No of digits: " + digit);
System.out.println("No of blank space: " + blank);
System.out.println("No of punctuatuion: " + punctuation);
}
}
output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.