advictoriam

Untitled

Nov 27th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.    A program reads in a string,
  5.    prints out the last character of the string,
  6.    followed by the first character, and then
  7.    followed by the middle three characters of the string.
  8.    All input strings should be of odd length.
  9. */
  10. public class StringGames
  11. {
  12.    public static void main (String[] args)
  13.    {
  14.       // Display prompt for input string
  15.       System.out.println("Please enter a string: ");
  16.  
  17.       // Read string
  18.       Scanner in = new Scanner(System.in);
  19.       String input = in.next();
  20.  
  21.       // Put together new string and print
  22.       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));
  23.       System.out.println(output);
  24.    }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment