Question

Your program must prompt the user to enter a string. The program must then test the...

Your program must prompt the user to enter a string. The program must then test the string entered by the user to determine whether it is a palindrome. A palindrome is a string that reads the same backwards and forwards, such as "radar", "racecar", and "able was I ere I saw elba". It is customary to ignore spaces, punctuation, and capitalization when looking for palindromes. For example, "A man, a plan, a canal. Panama!" is considered to be a palindrome.

To determine whether a string is a palindrome, you can: convert the string to lower case; remove any non-letter characters from the string; and compare the resulting string with the reverse of the same string. If they are equal, then the original string is considered to be a palindrome.

Here’s the code for computing the reverse of str:

String reverse;

  int i;

  reverse = "";

  for (i = str.length() - 1; i >= 0; i--) {

    reverse = reverse + str.charAt(i);

  }

As part of your assignment, you must write a static subroutine that finds the reverse of a string. The subroutine should have one parameter of type String and a return value of type String.

You must also write a second subroutine that takes a String as a parameter and returns a String. This subroutine should make a new string that is a copy of the parameter string, except that all the non-letters have been omitted. The new string should be returned as the value of the subroutine. You can tell that a character, ch is a lower-case letter by testing if (ch >= 'a' && ch <= 'z')

(Note that for the operation of converting str to lower case, you can simply use the built-in toLowerCase subroutine by saying: str = str.toLowerCase();)

You should write a descriptive comment before each subroutine, saying what it does.

Finally, write a main() routine that will read in a string from the user and determine whether or not it is a palindrome. The main routine should use The program should print the string converted to lower case and stripped of any non-letter characters. Then it should print the reverse string. Finally, it should say whether the string is a palindrome. (Use the two subroutines to process the user's string.) For example, if the user's input is "Hello World!", then the output might be:

   stripped: helloworld

    

    reversed: dlrowolleh

    

    This is NOT a palindrome.

    

    and if the input is "Campus motto: Bottoms up, Mac!", the output might be:

    

    stripped: campusmottobottomsupmac

    

    reversed: campusmottobottomsupmac  

    This IS a palindrome.

You must complete your program, test, debug, and execute it. You should test two different cases, one that is a palindrome and another one that is not a palindrome. You must submit your java code file (preferably by cut and paste the code into the assignment dialog box). The output of your program must be captured by either copying the content in the output window and pasting it into the assignment dialog box or by capturing the image of the screen which contains the output of your java program or the output and submitting this with your assignment as an attachment. In windows you can capture a screen shot with the Ctrl Alt and Print Screen key sequence. This image can then be pasted into a Word, WordPad, or OpenOffice document

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class Palindrome {

public static String reverse(String input) {

String rev = "";

for(int i=input.length()-1 ;i>=0; --i) {

rev = rev + input.charAt(i);

}

return rev;

}

public static String strip(String input) {

String stripped = "" ;

for(int i=0 ;i<input.length() ; ++i) {

if(input.charAt(i) >= 'a' && input.charAt(i) <= 'z')

stripped = stripped + input.charAt(i);

}

return stripped;

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the string: ");

String str = sc.nextLine();

str = str.toLowerCase();

String stripped = strip(str);

System.out.println("stripped: "+ stripped);

String rev = reverse(stripped);

System.out.println("reversed: "+ rev);

if(stripped.equals(rev)) {

System.out.println("This IS a palindrome.");

}else {

System.out.println("This is NOT a palindrome.");

}

}

}

=======================================
SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern. PLEASE UPVOTE

Add a comment
Know the answer?
Add Answer to:
Your program must prompt the user to enter a string. The program must then test the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this assignment, you will create a program that tests a string to see if it...

    For this assignment, you will create a program that tests a string to see if it is a palindrome. A palindrome is a string such as “madam”, “radar”, “dad”, and “I”, that reads the same forwards and backwards. The empty string is regarded as a palindrome. Write a recursive function: bool isPalindrome(string str, int lower, int upper) that returns true if and only if the part of the string str in positions lower through upper (inclusive at both ends) is...

  • Write a C Program that asks the user to input a string and then the user...

    Write a C Program that asks the user to input a string and then the user is prompted back whether the input string is a “Palindrome Word” or not. (A Palindrome Word reads the same backward or forward, eg. madam, racecar, etc.) Your program should contain a function that accepts the input string from the main function and returns a value indicating whether the input string is a Palindrome or not. Use Pointers to traverse the string.

  • Using Perl Program: Write a program with a subroutine to prompt the user for any message...

    Using Perl Program: Write a program with a subroutine to prompt the user for any message (like "what is your name?"). The sub will collect the user's answer and return it back to the call (in the main program above the subroutine). The sub does not accept any arguments. Print out the returned string (name) from the sub.

  • Write a program in MIPS to accept a string from a user, reverse that string and...

    Write a program in MIPS to accept a string from a user, reverse that string and print it out to the user. You must write a procedure that performs the reversal task. Example: Please Enter a String: Hello How Are You Reversed String: uoY erA woH olleH

  • Write a program that uses a recursive function to determine whether a string is a character-unit...

    Write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, the options for doing case sensitive comparisons and not ignoring spaces are indicated with flags. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive. Background Palindromes are character sequences...

  • Write a program that reverses a string and prints it on the screen. Sample input/output: Enter...

    Write a program that reverses a string and prints it on the screen. Sample input/output: Enter a string to be reversed: Testing the reverse string program reversed string: margorp gnirts esrever eht gnitseT

  • Write a program that prompts the user to input a string. The program then uses the...

    Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...

  • Prompt the user to enter two character strings. The program will then create a new string...

    Prompt the user to enter two character strings. The program will then create a new string which has the string that appears second, alphabetically, appended to the end of the string that shows up first alphabetically. Use a dash (-') to separate the two strings. You must implement the function string appendStrings(string, string), where the return value is the combined string. Display the newly created string. Your program must be able to handle both uppercase and lowercase characters. You are...

  • Python: Write a program that lets the user enter a string and displays the letter that...

    Python: Write a program that lets the user enter a string and displays the letter that appears most frequently in the string, ignore spaces, punctuation, and Upper vs Lower case. Create a list to hold letters based on the length of the user input Convert all letters to the same case Use a loop to check for each letter in the alphabet Have a variable to hold the largest number of times a letter appears, and replace the value when...

  • Program must be in Python 3. Write a program that prompts the user to enter two...

    Program must be in Python 3. Write a program that prompts the user to enter two strings and then displays a message indicating whether or not the first string starts with the second string. Your program must satisfy the following requirements. 1. Your program must include a function called myStartsWith that takes two string parameters. This function returns True if the rst string starts with the second string and returns False otherwise. 2. Your program must include a main function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT