Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. public class test {
  2.  
  3. public static void main(String[] args)
  4. {
  5. StringBuffer sp_string = new StringBuffer("AbCdEf");
  6. swapPairs(sp_string);
  7. System.out.println(sp_string);
  8. }
  9.  
  10. public static void swapPairs(StringBuffer s)
  11. {
  12. int s_len = s.length();
  13.  
  14. // This covers zero and 1 length strings.
  15. if (s_len < 2) {
  16. return;
  17. }
  18.  
  19. char stash = ' ';
  20.  
  21. for (int i = 0; i < s_len; i += 2) {
  22. if (i + 2 > s_len) {
  23. break;
  24. }
  25.  
  26. stash = s.charAt(i);
  27. s.setCharAt(i, s.charAt(i+1));
  28. s.setCharAt(i + 1, stash);
  29. }
  30.  
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement