Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. /*ReverseString
  2. * */
  3.  
  4. import java.util.Scanner;
  5. public class reversestring {
  6. public static void main(String[]args) {
  7. String inStr;//input string
  8. String reverse = "";
  9. int inStrLen; //length of the input String
  10.  
  11. Scanner in = new Scanner(System.in);
  12. System.out.print("Enter a String: ");
  13. inStr = in.next(); //use next() to read a string
  14. inStrLen = inStr.length();
  15.  
  16. //use inStr.charAt(index) in a loop to extract character at "index" from inStr
  17. // The string's index begins at 0 from the left.
  18. for (int i = inStrLen - 1; i >= 0; --i) { //Process the
  19. String from the right
  20. reverse = reverse + inStr.charAt(i);
  21. System.out.print("The reverse of string "+ inStr + " is "
  22. + reverse);
  23. }
  24.  
  25.  
  26. }
  27. }
  28.  
  29. Enter a String: 234
  30. The reverse of string 234 is 4The reverse of string 234 is 43The
  31. reverse of string 234 is 432
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement