Java reverse string without using a temporary string, Char or String Builder

Is it possible to flip String in Java without using any temporary variables like String , Char[] or StringBuilder ?

Only use int or int[] .

+6
source share
11 answers
 String reverseMe = "reverse me!"; for (int i = 0; i < reverseMe.length(); i++) { reverseMe = reverseMe.substring(1, reverseMe.length() - i) + reverseMe.substring(0, 1) + reverseMe.substring(reverseMe.length() - i, reverseMe.length()); } System.out.println(reverseMe); 

Conclusion:

 !em esrever 

Just for fun, of course, using StringBuffer would be better, here I am creating new lines for each iteration, the only difference is that I am not presenting a new link, and I only have an int counter.

+8
source

Objects of the Java String class are immutable - their contents cannot be changed after creation.

You will need at least two temporary objects: one for the final result and one for intermediate values, even if you find a way to avoid using a local variable.

EDIT:

However, since you can use int[] , you can cheat.

Since char can be assigned int , you can use String.charAt() to create an int array with character values ​​in the reverse order. Or you may be allowed to use String.toCharArray() to get a char array that will be copied to your int[] temporary.

Then you use a variable containing a reference to the source string (or a result variable if allowed) to start with an empty string (easy to get with the direct purpose or String.substring() ) and use String.concat() to create the final result .

In any case, you cannot replace characters in place, as it would in C / C ++.

EDIT 2:

Here is my version that does not use StringBuffer / Builders inside:

 int r[] = new int[s.length()]; int idx = r.length - 1; for (int i : s.toCharArray()) { r[idx--] = i; } s = s.substring(0, 0); for (int i : r) { s = s.concat(String.valueOf((char)i)); } 
+10
source

One of many ways:

  String str = "The quick brown fox jumps over the lazy dog"; int len = str.length(); for (int i = (len-1); i >= 0; --i) str += str.charAt(i); str = str.substring(len); System.out.println(str); 
+5
source
 String s = "Hello World!"; for(int i = 0; i < s.length(); i++) { s = s.substring(1, s.length() - i) + s.charAt(0) + s.substring(s.length() - i); } System.out.println(s); // !dlroW olleH 

No temporary variables! :)

+3
source

Since you can use int, you can assign the value int a char:

 String aString = "abc"; int intChar = aString.charAt(0); 

You will need to convert from int back to char to assign it to aString.charAt (2).

I'm sure you can figure it out from there.

+1
source

First, add the line to yourself in the opposite way. Then take the other half.

  public class RevString { public static void main(String[] args) { String s="string"; for(int i=s.length()-1;i>=0;i--){ s+=s.charAt(i); } s=s.substring(s.length()/2, s.length()); System.out.println(s); } } 
+1
source
 public String reverseStr(String str) { if (str.length() <= 1) { return str; } return reverseStr(str.substring(1)) + str.charAt(0); } 
+1
source

You can use the java.lang.StringBuilder class:

 String reservedString = new StringBuilder(str).reserve().toString(); 
0
source

Without using any collection, a StringBulider, StringBuffer, or temp array reverses the string. Simple and clear:

 public static void main(String[] args) { String test = "Hello World"; String rev = ""; Pattern p = Pattern.compile("[\\w|\\W]"); Matcher m = p.matcher(test); while (m.find()) { rev = m.group()+rev; } System.out.println("Reverse==" + rev); } 

Exit

Inverse == dlroW olleH

Hope this helps :)

0
source
 public class Test { static St`enter code here`ring reverseString(String str) { for (int i = 0; i < str.length() / 2; i++) { if (i == 0) { str = str.charAt(str.length() - 1 - i) + str.substring(i + 1, str.length() - 1 - i) + str.charAt(i); } else { str = str.substring(0, i) + str.charAt(str.length() - 1 - i) + str.substring(i + 1, str.length() - 1 - i) + str.charAt(i) + str.substring(str.length() - i, str.length()); } } return str; } public static void main(String args[]) { String s = "ABCDE"; System.out.println(Test.reverseString(s)); } } 
0
source
 String str = "Welcome"; for(int i=0;i<str.length();){ System.out.print(str.charAt(str.length()-1)); str = str.substring(0,str.length()-1); } 

Excluding loop variables.

0
source

Source: https://habr.com/ru/post/898384/


All Articles