t805

Reverse String's First And Last Character

Sep 10th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. package week3;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Question2 {
  6.    
  7.     public static void main(String[] args) {
  8.        
  9.         Scanner input = new Scanner(System.in);
  10.        
  11.         String s1;
  12.        
  13.         do {
  14.            
  15.             System.out.println("Enter a string: ");
  16.             s1 = input.nextLine();
  17.  
  18.             System.out.println("Your string is: " + s1);
  19.  
  20.             StringBuffer s2 = new StringBuffer(s1);
  21.  
  22.             char temp = s2.charAt(0);
  23.  
  24.             s2.setCharAt(0, s2.charAt(s2.length()-1));
  25.             s2.setCharAt(s2.length()-1, temp);
  26.  
  27.             String s3 = s2.toString();
  28.  
  29.             System.out.println("Your new string is: " + s3);
  30.        
  31.         }while(!"quit".equals(s1));
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment