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

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
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
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 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...
Write a program that prompts the user to input their first name from the keyboard and...
Write a program that prompts the user to input their first name from the keyboard and stores them in the variable "firstName". It does the same for last name and stores it in the variable "lastName". It then uses strcat to merge the two names, separates them by a space and stores the full name into a string variable called "fullName". In the end, the program must print out the string stored within fullName. ANSWER IN C LANGUAGE ! You...
JAVA. A palindrome is a word or a phrase that is the same when read both forward and backward.
java please. 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: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.Hint: Start by just handling single-word...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard...
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard by displaying the message on the screen to ask and read the following information: * customer ID (string) * customer name (string)                                                        * balance (float) -open output file customer .txt to write -Write to the output file customer.txt the following information:                 Customer ID – customer name – balance For example: 1561175753 - James Smith – 1255.25 -close file QUESTION 5: -create one notepad...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT