Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. public static String reverse(String word) {
  2.  
  3. if (word == null || "".equals(word) || word.length() == 1) {
  4. throw new IllegalArgumentException("Invalid Entry");
  5. }
  6.  
  7. StringBuilder result = new StringBuilder();
  8.  
  9. for (int i=word.length() - 1; i >= 0; i--) {
  10. result.append(word.charAt(i));
  11. }
  12.  
  13. return result.toString();
  14. }
  15.  
  16. public static String reverse ( String s ) {
  17. int length = s.length(), last = length - 1;
  18. char[] chars = s.toCharArray();
  19. for ( int i = 0; i < length/2; i++ ) {
  20. char c = chars[i];
  21. chars[i] = chars[last - i];
  22. chars[last - i] = c;
  23. }
  24. return new String(chars);
  25. }
  26.  
  27. public void reverse(char[] chars) {
  28. for (int i = 0, j = chars.length - 1; i < j; i++, j--) {
  29. char temp = chars[i];
  30. chars[i] = chars[j];
  31. chars[j] = temp;
  32. }
  33. }
  34.  
  35. class ReversedString {
  36.  
  37. private String string;
  38.  
  39. public ReversedString(String string) {
  40. this.string = string;
  41. }
  42.  
  43. public char charAt(int index) {
  44. return string.charAt(string.length() - 1 - index);
  45. }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement