There are many ways to reverse a String in Java. We will see traditional ways and using classes that provide anĀ in-build function.

1. Storing in reverse Order
public static String reverse(String a) {
char[] newWord = new char[a.length()];
for (int i = 0, j = a.length() - 1; i < a.length(); i++, j--) {
newWord[j] = a.charAt(i); // Reading from first and adding to last
}
return new String(newWord);
}

// Method Call
reverse("reverse");

// Breaking each loop iteration logic
i=0; j=6; => newWord[6] = r => newWord[ _, _, _, _, _, _, r ]
i=1; j=5; => newWord[5] = e => newWord[ _, _, _, _, _, e, r ]
i=2; j=4; => newWord[4] = v => newWord[ _, _, _, _, v, e, r ]
i=3; j=3; => newWord[3] = e => newWord[ _, _, _, e, v, e, r ]
i=4; j=2; => newWord[2] = r => newWord[ _, _, r, e, v, e, r ]
i=5; j=1; => newWord[1] = s => newWord[ _, s, r, e, v, e, r ]
i=6; j=0; => newWord[0] = e => newWord[ e, s, r, e, v, e, r ]

In the above piece of code.

  • We have assigned a char of length of String passed to it.
  • In the loop we are taking a character from start index and adding it to the last index of above-assigned array.
  • Finally, we combine all Character as String and return
2. Dividing into Half and Swapping
public static String reverse(String orig)
{
char[] s = orig.toCharArray();
int n = s.length;
int halfLength = n / 2;
for (int i=0; i<halfLength; i++)
{
char temp = s[i];
s[i] = s[n-1-i];
s[n-1-i] = temp;
}
return new String(s);
}

// Method Call
reverse("reverse");


s = [r, e, v, e, r, s, e]
n = 7
halfLength = 7/2 = Floor of value. 3.5 => 3
// Breaking each loop iteration logic
i=0; i < 3; temp = 'r'; s => s[0] = ['e', e, v, e, r, s, e]; s[6] = 'r'; Final => s = ['e', e, v, e, r, s, 'r']
i=1; i < 3; temp = 'e'; s => s[1] = ['e', 's', v, e, r, s, e]; s[6] = 'e'; Final => s = ['e', 's', v, e, r, 'e', 'r']
i=2; i < 3; temp = 'v'; s => s[2] = ['e', 's', 'r', e, r, s, e]; s[6] = 'v'; Final => s = ['e', 's', 'r', e, 'v', 'e', 'r']

In the above piece of code.

  • We first copy String value as Character to another variable.
  • Loop through just half the length;
  • In 0th iteration, we swap 0 index value with the 6th index value.
  • In 1st iteration, we swap 1st index value with 5th index value and so on…
Using StringBuffer and StringBuilder
// Using StrinBuilder
new StringBuilder(word).reverse().toString()

// using StringBuffer
new StringBuffer(word).reverse().toString()


// This is internal Logic of StringBuilder#reverse
public AbstractStringBuilder reverse() {
boolean hasSurrogate = false;
int n = count - 1;
for (int j = (n-1) >> 1; j >= 0; --j) {
char temp = value[j];
char temp2 = value[n - j];
if (!hasSurrogate) {
hasSurrogate = (temp >= Character.MIN_SURROGATE && temp <= Character.MAX_SURROGATE)
|| (temp2 >= Character.MIN_SURROGATE && temp2 <= Character.MAX_SURROGATE);
}
value[j] = temp2;
value[n - j] = temp;
}
if (hasSurrogate) {
// Reverse back all valid surrogate pairs
for (int i = 0; i < count - 1; i++) {
char c2 = value[i];
if (Character.isLowSurrogate(c2)) {
char c1 = value[i + 1];
if (Character.isHighSurrogate(c1)) {
value[i++] = c1;
value[i] = c2;
}
}
}
}
return this;
}
Categories: JAVA

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *