Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // By: Sammy Samkough
- import java.util.Scanner;
- /**
- Write a program that will allow the user to to input his name.
- Using a for loop and the substring method, produce a printout
- of the reversal of the name.
- */
- public class NameReversal
- {
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- System.out.print("Please enter your name. ");
- String name = sc.nextLine();
- String nameTwo = ""; // when we reverse the name it needs to be in a variable
- int k = name.length();
- System.out.println("The name is " + name + ".");
- for (int i = k - 1; i >= 0; i--)
- {
- // Use substring to get the last letter but the for loop
- // will do that for us so we just have to do it once.
- // Remember, substring starts at 0!
- nameTwo += name.substring(i, i + 1);
- }
- System.out.println("The reversed name lowercased is " + nameTwo.toLowerCase() + ".");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment