Friday, 19 August 2016

Chapter 5 Exercise 46, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*5.46 (Reverse a string) Write a program that prompts the user to enter a string and displays the string in reverse order.

Enter a string: ABCD
The reversed string is DCBA

import java.util.Scanner;
 
public class ProgrammingEx5_46 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  // Enter saving amount
  System.out.print("Enter a string:");
  String s = input.nextLine();
  String s2 = "";
 
  for (int i = s.length() - 1; i >= 0; i--) {
   s2 += s.charAt(i);
 
  }
 
  System.out.println("The reversed string is " + s2);
 
 }
 
}

No comments :

Post a Comment