Crenox

NameReversal Program

Feb 10th, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. // By: Sammy Samkough
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6. Write a program that will allow the user to to input his name.
  7. Using a for loop and the substring method, produce a printout
  8. of the reversal of the name.
  9. */
  10. public class NameReversal
  11. {
  12. public static void main(String[] args)
  13. {
  14. Scanner sc = new Scanner(System.in);
  15.  
  16. System.out.print("Please enter your name. ");
  17. String name = sc.nextLine();
  18. String nameTwo = ""; // when we reverse the name it needs to be in a variable
  19. int k = name.length();
  20.  
  21. System.out.println("The name is " + name + ".");
  22.  
  23. for (int i = k - 1; i >= 0; i--)
  24. {
  25. // Use substring to get the last letter but the for loop
  26. // will do that for us so we just have to do it once.
  27. // Remember, substring starts at 0!
  28. nameTwo += name.substring(i, i + 1);
  29. }
  30. System.out.println("The reversed name lowercased is " + nameTwo.toLowerCase() + ".");
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment