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.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Question: 

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:

palindrome: bob

Ex: If the input is:

bobby

the output is:

not a palindrome: bobby

Hint: Start by just handling single-word input, and submit for grading. Once passing single-word test cases, extend the program to handle phrases. If the input is a phrase, remove or ignore spaces.

 

My Issue:

I'm unable to use replaceAll(" ", "") as the java program wouldn't allow me as we're working on loops this week and haven't touch upon it. I've attached a screenshot where replaceAll method resulted in no output with the error displayed "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 16". Regardless,I don't know how to include the space in the string variable to include the entire sentence as a palindrone. 

My code: 

import java.util.Scanner;

public class LabProgram {
public static void main(String[] args) {
/* Type your code here. */String word="";
String opposite = "";
Scanner scnr= new Scanner(System.in);
word = scnr.nextLine();


for(int i = word.length() - 1; i >= 0; i--){
opposite = opposite + word.charAt(i);
}
if(word.equals(opposite)){
System.out.println(“palindrome: " +word);
}

else{
System.out.println(“not a palindrome: "+word);
}
}
}

My initial code with replaceAll: 

String word = "";
     String oppos = "";
        Scanner scnr = new Scanner(System.in);
        word = scnr.nextLine();
        String str = word.replaceAll(" ","");
        
        for(int i = word.length() - 1; i >= 0; i--){
            oppos = oppos + str.charAt(i);
        }
        if(str.equals(oppos)){
            System.out.println(“palindrome: " +word);
        }

        else{
            System.out.println(“not a palindrome: "+word);
        }
    }
}

1: Compare output ^
1/1
Input
bob
Your output
palindrome: bob
2: Compare output ^
1/1
Input
sees
Your output
palindrome: sees
3: Compare output ^
0/1
Output differs. See highlights below.
Special character legend
Input
never odd or even
Your output
not a palindrome: never odd or even
Expected output
palindrome: never odd or even
Transcribed Image Text:1: Compare output ^ 1/1 Input bob Your output palindrome: bob 2: Compare output ^ 1/1 Input sees Your output palindrome: sees 3: Compare output ^ 0/1 Output differs. See highlights below. Special character legend Input never odd or even Your output not a palindrome: never odd or even Expected output palindrome: never odd or even
1: Compare output ^
1/1
Input
bob
Your output
palindrome: bob
2: Compare output a
1/1
Input
sees
Your output
palindrome: sees
3: Compare output ^
0/1
Exception in thread "main" java.lang.StringIndexOutofBoundsException: String index out of
at java.base/java.lang.StringLatinl.charAt (StringLatinl.java:47)
at java.base/java.lang.String.charAt (String.java:693)
at LabProgram.main (LabProgram.java:12)
Input
never odd or even
Your output Your program produced no output
Expected output
palindrome: never odd or even
4: Compare output ^
1/1
Transcribed Image Text:1: Compare output ^ 1/1 Input bob Your output palindrome: bob 2: Compare output a 1/1 Input sees Your output palindrome: sees 3: Compare output ^ 0/1 Exception in thread "main" java.lang.StringIndexOutofBoundsException: String index out of at java.base/java.lang.StringLatinl.charAt (StringLatinl.java:47) at java.base/java.lang.String.charAt (String.java:693) at LabProgram.main (LabProgram.java:12) Input never odd or even Your output Your program produced no output Expected output palindrome: never odd or even 4: Compare output ^ 1/1
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY