Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String reverse(String word) {
- if (word == null || "".equals(word) || word.length() == 1) {
- throw new IllegalArgumentException("Invalid Entry");
- }
- StringBuilder result = new StringBuilder();
- for (int i=word.length() - 1; i >= 0; i--) {
- result.append(word.charAt(i));
- }
- return result.toString();
- }
- public static String reverse ( String s ) {
- int length = s.length(), last = length - 1;
- char[] chars = s.toCharArray();
- for ( int i = 0; i < length/2; i++ ) {
- char c = chars[i];
- chars[i] = chars[last - i];
- chars[last - i] = c;
- }
- return new String(chars);
- }
- public void reverse(char[] chars) {
- for (int i = 0, j = chars.length - 1; i < j; i++, j--) {
- char temp = chars[i];
- chars[i] = chars[j];
- chars[j] = temp;
- }
- }
- class ReversedString {
- private String string;
- public ReversedString(String string) {
- this.string = string;
- }
- public char charAt(int index) {
- return string.charAt(string.length() - 1 - index);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement