JAVA - Return string without temp String, array, Stringbuilder, substring

I have a question for you. So, I need to change the line with this requirement:

  • Use only: 1 variable string (for input), 1 char variable and 1 int variable
  • You cannot use another row, row builder, list, array, or collection.
  • Output result assigned to input variable
  • The length of the input variable cannot be increased.
  • Unable to use function substring

And then I try it, is it right?

String str = "Hello World"; System.out.println("Before : "+str); for(int i=0;i<str.length();i++){ str = new String(str.getBytes(), 1, str.length()-1-i) + new String(str.getBytes(), 0, 1) + new String(str.getBytes(), str.length()-i, i); System.out.println(str); } System.out.println("After : "+str); 

Exit:

Before: Hello World

ello WorldH

llo worldeh

lo WorldleH

o WorldlleH

Worldolleh

World olleH

orldW olleH

rldoW olleH

ldroW olleH

dlroW olleH

dlroW olleH

After: dlroW olleH

+1
source share
4 answers

You can use recursion for the task, something like this:

 public static String reverse(String source, int from) { if (source.length()-from == 1) { return source.charAt(from)+""; } return reverse(source, from+1) + source.charAt(from); } 
+2
source

String is an immutable class in java, any methods that seem to modify it always return a new string object with a modification. So the answer is: NO, you cannot undo String in place in Java. Java String are implemented as wrappers around a char array that is hidden from you (i.e. you can only get copies of this array in the usual way).

+1
source

Use only: 1 variable string (for input), 1 char variable and 1 int variable

It follows that the desired solution:

  • Take a character from position n of a String, store it in a character variable.
  • Move the variable m to n (per line).
  • restore cached char with character variable to position m .
  • repeat until the line is fully processed.

This is a typical question asked for "basic" programming languages. However, Java does not allow this, since it eliminates the possibility of setting a character based on a position within a string.

Although this is not a problem in other languages, java does not have the ability to set index-based values ​​for strings.

 public static String reverse(String str){ char c; int i = 0; for (i=0; i< str.length() / 2; i++){ c = str.charAt(i); //this would be required to match your requirements. str[i] = str.charAt(str.length() -1 -i); str[str.length() -1 -i] = c; } return str; } 

but in java you can only do:

 public static String reverse(String str){ char c; int i = 0; for (i=0; i< str.length() / 2; i++){ c = str.charAt(i); char[] temp = str.toCharArray(); temp[i] = str.charAt(str.length() -1-i); temp[str.length() -1 -i] = c; str = new String(temp); } return str; } 

which creates additional char arrays and new String objects ... Even if you can omit this into one additional char array by declaring it from a for loop, it no longer meets the requirement.

I think the guy asking the question thought in "C" or "php" where index-based access to strings is possible.

If String is equal to char -array (which it does in some older languages, which may be the source of this exercise), it will look like this:

 public static char[] reverse(char[] str){ char c; int i = 0; for (i=0; i< str.length / 2; i++){ c = str[i]; str[i] = str[str.length -1-i]; str[str.length -1 -i] = c; } return str; } 
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

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


All Articles