Question

In: Computer Science

Input a phrase from the keyboard and output whether or not it is a palindrome. For...

Input a phrase from the keyboard and output whether or not it is a palindrome.

For example:

1. If the input is

Madam I'm Adam

Your output should be

"Madam, I'm Adam" is a palindrome

2. If your input is

Going to the movies?

Your output should be

"Going to the movies?" is not a palindrome

Note the quotes in the output line.

Code language java using JGrasp

Solutions

Expert Solution

Program to check whether string is Palindrome or not:

Code and Screenshots are attached.

Note: Output in double quotes is important, we need to insert comma after first word if entered string is palindrome.

import java.util.*;
public class Palindrome
{ 
    // To check sentence is palindrome or not 
    static boolean checkPalindrome(String str) 
    {     
        int fromStart = 0; 
        int fromEnd = str.length()-1; 
          
        // Lowercase string 
        str = str.toLowerCase(); 
          
        // Compares character until they are equal 
        while(fromStart <= fromEnd) 
        { 
              
            char getAtStart = str.charAt(fromStart); 
            char getAtEnd = str.charAt(fromEnd); 
              
         
            if (!(getAtStart >= 'a' && getAtEnd <= 'z')) 
                fromStart++; 
              
         
            else if(!(getAtEnd >= 'a' && getAtEnd <= 'z')) 
                fromEnd--; 
              
            // If characters are equal 
            else if( getAtStart == getAtEnd) 
            { 
                fromStart++; 
                fromEnd--; 
            } 
              
            // If characters are not equal then  sentence is not a palindrome 
            else 
                return false; 
        } 
          
        // Returns true if  sentence is palindrome 
        return true;     
    } 
      
    public static void main(String[] args)  
    { 
         Scanner sc= new Scanner(System.in); //System.in is a standard input stream.
         System.out.print("Enter a string to check whether it is a Palindrome: ");
         String str= sc.nextLine();
          if( checkPalindrome(str)){
          if(str.contains(" ")){
           String  firstWord= null;
           firstWord= str.substring(0, str.indexOf(" "));  
            System.out.println("\""+firstWord+ ","+str.substring((firstWord.length()+1), str.length())+"\""+" is a palindrome"); 
          }}
          else
          System.out.println("\""+ str+ "\"" + 
                                         " is not a palindrome"); 
     
} 
}
 

Screenshots


Related Solutions

Input a phrase from the keyboard. If the last letter in the phrase is "a" or...
Input a phrase from the keyboard. If the last letter in the phrase is "a" or "e" (uppercase or lowercase) then output the last letter 3 times. ELSE If the first letter in the phrase is either "i", "o", or "u" (uppercase or lowercase) then output the length of the phrase In all other cases print "no vowel" Code language is java
Input a phrase from the keyboard. For example, input could be: Today is a rainy day,...
Input a phrase from the keyboard. For example, input could be: Today is a rainy day, but the sun is shining. Your program should count (and output) the number of "a"s in the phrase, the number of "e"s, the number of "i"s , the number of "o"s, the number of "u"s, and the total number of vowels. Note: The number of "a"s means the total of uppercase and lowercase "a"s etc. Also: the vowels are a,e,i,o,u. In the above example...
Write a program that determines whether an input string is a palindrome; that is, whether it...
Write a program that determines whether an input string is a palindrome; that is, whether it can be read the same way forward and backward. At each point, you can read only one character of the input string; do not use an array to first store this string and then analyze it (except, possibly, in a stack implementation). Consider using multiple stacks. In Pseudocode please
Create using Java Description: Palindrome -- According to wikipedia "A palindrome is a word, phrase, number...
Create using Java Description: Palindrome -- According to wikipedia "A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction" Write a application that can determine if a 5 digit number you input is a palindrome. If the number is a palindrome then print "The number is a palindrome." If it is not then print "The number is NOT a palindrome" Make sure to use an array Allow input...
Write a driver to get a String input from keyboard and if the input string has...
Write a driver to get a String input from keyboard and if the input string has less than 10 characters, throw a StringTooShortException. public class StringTooShortException extends Exception {     //-----------------------------------------------------------------     // Sets up the exception object with a particular message.     //-----------------------------------------------------------------     public StringTooShortException()     {         super("String does not have enough characters");     } }
Write an X86-series assembly language program that checks whether input string is palindrome or not. A...
Write an X86-series assembly language program that checks whether input string is palindrome or not. A palindrome is a word, number, phrase or any other sequence which reads the same backward as forward e.g. madam, racecar. Sample Execution: Please enter a String: redivider The string is a palindrome Another Sample Execution: Please enter a String: abracadabra The string is not a palindrome
A palindrome is a word or a phrase that is the same when read both forward...
A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome. Ex: If the input is: bob the output is: bob is a palindrome Ex: If the input is: bobby the output is: bobby is not a palindrome Hint: Start by just handling...
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.
# PYTHONWrite a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.Ex: If the input is:n Mondaythe output is:1Ex: If the input is:z Today is Mondaythe output is:0Ex: If the input is:n It's a sunny daythe output is:2Case matters.Ex: If the input is:n Nobodythe output is:0n is different than N.
A palindrome is a word or a phrase that is the same when read both forward and backward.
6.7 LAB: PalindromeA palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome.Ex: If the input is:bobthe output is:bob is a palindromeEx: If the input is:bobbythe output is:bobby is not a palindromeHint: Start by removing spaces. Then check if a string is equivalent to it's reverse.This is my code:s =...
A palindrome is a word or phrase, which reads the same backward or forward. Write a...
A palindrome is a word or phrase, which reads the same backward or forward. Write a program that prompts the user for a string of characters terminated by a period and determines whether the string (without the period) is a palindrome. IMP: Assume that the input contains only letters and blanks. Assume also that the input is at most 30 characters long. Use an array of characters of size 30 to store the input! Disregard blanks when deciding if the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT