Transposing and wrapping a string in java

I worked on two methods, which will be Transpose and Untranspose String respectively. The solutions I came up with work as far as I know. I just want to know if I could solve these problems in a simpler way. My code seems too long for the task to be performed. The first method, transpose (), will take String as a parameter and transfer it. If "bridge" is entered, the output will be "bergid". Similarly, using the unTranspose () method, if the user enters "bergid", the output will be a "bridge".

public void transpose( String s ) { String t = ""; int end = s.length() - 1; for ( int i = 0; i < s.length() / 2; i++ ) { t += Character.toString( s.charAt( i ) ) + Character.toString( s.charAt( end ) ); end--; } // Lenth of String is odd if ( s.length() % 2 == 1 ) { // add character in middle of String to the end of the new String t+= Character.toString( s.charAt( s.length() / 2 ) ); } System.out.println( t ); } public void unTranspose( String s ) { String t = ""; // Length of String is odd if ( s.length() % 2 == 1 ) { for ( int i = 0; i < s.length(); i+=2 ) { t+= Character.toString( s.charAt( i ) ); } for ( int i = s.length() - 2; i > 0; i -= 2 ) { t += Character.toString( s.charAt( i ) ); } System.out.println( t ); } // Length of String is even else if ( s.length() % 2 == 0 ) { for ( int i = 0; i < s.length() - 1; i+=2 ) { t+= Character.toString( s.charAt( i ) ); } for ( int i = s.length() - 1; i > 0; i -= 2 ) { t+= Character.toString( s.charAt( i ) ); } System.out.println( t ); } } 

My code looks awful. I'm still not used to formatting my code correctly. Please carry me.

thank you for your time


Definition

  transpose ---------> "123Xcba" "1a2b3cX" <----------- untranspose 
+4
source share
7 answers

Using recursion

 public static String transpose(String str) { if (str == null || str.length() == 1 || str.length() == 2) { return str; } else { return str.substring(0, 1) + str.substring(str.length() -1, str.length()) + transpose(str.substring(1, str.length() -1) ); } } public static String untranspose(String str) { if (str == null || str.length() == 1 ||str.length() == 2) { return str; } else { return str.substring(0, 1) + untranspose(str.substring(2, str.length())) + str.substring(1, 2); } } 
+4
source

This solution has symmetry symmetry.

 public static String transpose(String s) { StringBuilder sb = new StringBuilder(); sb.setLength(s.length()); for (int i = 0, j = s.length() - 1, x = 0; i <= j; ) { sb.setCharAt(x++, s.charAt(i++)); if (i > j) break; sb.setCharAt(x++, s.charAt(j--)); } return sb.toString(); } public static String untranspose(String s) { StringBuilder sb = new StringBuilder(); sb.setLength(s.length()); for (int i = 0, j = s.length() - 1, x = 0; i <= j; ) { sb.setCharAt(i++, s.charAt(x++)); if (i > j) break; sb.setCharAt(j--, s.charAt(x++)); } return sb.toString(); } 

This makes it obvious that the logic between the two methods is identical; The only difference is that:

  • transpose , i and j indicate read indices, x is the write index
  • In untranspose , i and j are the write indices, x is the read index (i.e. vice versa)

It is really quite simple:

  • i always goes from the beginning to the middle of the line
  • j always goes from the end to the middle of the line
  • x always goes from beginning to end of line
  • If the input is of odd length, i == j will inevitably end up
    • At this point you will need i , so break

Lalit came up with the first recursive solution ; this is the same thing with a slight modification:

 public static String transpose(String s) { int L = s.length(); return (L < 2) ? s : s.substring(0, 1) + s.substring(L-1, L) + transpose(s.substring(1, L-1)); } public static String untranspose(String s) { int L = s.length(); return (L < 2) ? s : s.substring(0, 1) + untranspose(s.substring(2, L)) + s.substring(1, 2); } 
+3
source

Here is my answer to this question. The main problem that I saw with your code is that you create Strings as temporary objects in several areas of the code - this makes it very inefficient and also very slow. Another problem is that you want to exteriorize everything you can from loops. I compiled it and ran it and it works.

 package com.rch.test; public class Transposer { public static String transpose(String s) { int length = s.length(); int end = length - 1; StringBuilder t = new StringBuilder(); for (int i = 0; i < length / 2; i++) { t.append(s.charAt(i)); t.append(s.charAt(end)); end--; } // Length of String is odd if (length % 2 == 1) { // add character in middle of String to the end of the new String t.append(s.charAt(length / 2)); } return t.toString(); } public static String unTranspose(String s) { int length = s.length(); StringBuilder t = new StringBuilder(); if (length % 2 == 1) { for (int i = 0; i < length; i += 2) { t.append(s.charAt(i)); } for (int i = length - 2; i > 0; i -= 2) { t.append(s.charAt(i)); } } else if (length % 2 == 0) { for (int i = 0; i < length - 1; i += 2) { t.append(s.charAt(i)); } for (int i = length - 1; i > 0; i -= 2) { t.append(s.charAt(i)); } } return t.toString(); } public static void main(String[] args) { String testString = "bridge"; String transposedString = Transposer.transpose(testString); String finalString = Transposer.unTranspose(transposedString); System.out.println("1)" + testString); System.out.println("2)" + transposedString); System.out.println("3)" + finalString); } } 

Exit: 1) bridge 2) bergid 3) bridge

+1
source

quickly executed your transpose method with StringBuilder, which usually simplifies these operations. This seems to work with an example of your bridge, as well as with odd-length strings.

 public static void transpose( String s ) { StringBuilder sb = new StringBuilder(s); for( int i=1; i<sb.length(); i=i+2 ) { sb.insert( i, sb.charAt( sb.length()-1 ) ); sb.deleteCharAt( sb.length()-1 ); } System.out.println( sb.toString() ); } 

Should give you enough ideas to implement the intransitive method yourself: +)

0
source

Here is a method that does not require other behavior, based on whether the string length is even or odd.

 public static String transpose(String in) { StringBuilder out = new StringBuilder(); for (int i=0; i<in.length(); ++i) { out.append(in.charAt(i)); out.append(in.charAt(in.length() - i - 1)); } return out.substring(0, in.length()); } public static String untranspose(String in) { StringBuilder out = new StringBuilder(); for (int i=0; i<in.length(); i+=2) { out.append(in.charAt(i)); } StringBuilder reversedSecondHalf = new StringBuilder(); for (int i=1; i<in.length(); i+=2) { reversedSecondHalf.append(in.charAt(i)); } out.append(reversedSecondHalf.reverse()); return out.toString(); } 
0
source

Well, I simplified the transpose method a bit:

 public static String transpose(String s) { StringBuilder sb = new StringBuilder(); int i = 0; int length = s.length() - 1; while(i < length - i) { sb.append(s.charAt(i)).append(s.charAt(length - i)); i++; } if(i == length - i) sb.append(s.charAt(i)); return sb.toString(); } 

Update

Tried my luck with a non-transnet -

 public static String untranspose(String s) { StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); int length = s.length(); int iopp = (length % 2 == 0) ? length - 1 : length - 2; for(int i = 0; i < length; i += 2, iopp -= 2) { sb1.append(s.charAt(i)); if(iopp >= 0) sb2.append(s.charAt(iopp)); } return sb1.append(sb2).toString(); } 
0
source

Here is another solution that shows the symmetry between transposition and non-transposition.

 public static String transpose(String in) { int length = in.length(); StringBuilder out = new StringBuilder(in); for (int pos=1; pos<length; pos+=2) { swapCharacters(out, length-1, pos); } return out.toString(); } public static String untranspose(String in) { int length = in.length(); StringBuilder out = new StringBuilder(in); for (int pos=length-1-(length%2); pos>0; pos-=2) { swapCharacters(out, pos, length-1); } return out.toString(); } private static void swapCharacters(StringBuilder string, int oldPos, int newPos) { char c = string.charAt(oldPos); string.deleteCharAt(oldPos); string.insert(newPos, c); } 
0
source

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


All Articles