Question

Write a program that accepts a String value from the user and displays the reverse of...

Write a program that accepts a String value from the user and displays the reverse of that value. For additional challenge, determine if the String and its reverse are equal and display a message explaining the result.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

//ReverseString.java
class ReverseString{

  public static String reverse(String str){
    if(str.length() == 0){
      return "";
    }
    else{
      return str.charAt(str.length()-1)+reverse(str.substring(0,str.length()-1));
    }
  }
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.printf("Enter string: ");
    String str = scanner.nextLine();
    String reversedString = reverse(str);
    System.out.println("Reversed: "+reversedString);
    if(str.equals(reversedString)){
      System.out.println("String and its reverse are equal");
    }
    else{
      System.out.println("String and its reverse are NOT equal");
    }
  }
}

Add a comment
Know the answer?
Add Answer to:
Write a program that accepts a String value from the user and displays the reverse of...
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
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