Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- A program reads in a string,
- prints out the last character of the string,
- followed by the first character, and then
- followed by the middle three characters of the string.
- All input strings should be of odd length.
- */
- public class StringGames
- {
- public static void main (String[] args)
- {
- // Display prompt for input string
- System.out.println("Please enter a string: ");
- // Read string
- Scanner in = new Scanner(System.in);
- String input = in.next();
- // Put together new string and print
- String output = String.format("%s%s%s%s%s", input.charAt(input.length()-1), input.charAt(0), input.charAt((input.length()/2)-1), input.charAt((input.length()/2)), input.charAt((input.length()/2)+1));
- System.out.println(output);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment