14.7 C
London
Monday, September 9, 2024

Fixing Easy Transposition in Java


The problem

Easy transposition is a fundamental and easy cryptography method. We make 2 rows and put first a letter in Row 1, the second in Row 2, third in Row 1, and so forth till the top. Then we put the textual content from Row 2 subsequent to the Row 1 textual content and that’s it.

Full the perform that receives a string and encrypts it with this easy transposition.

Instance:

For instance, if the textual content to encrypt is: "Easy textual content", the two rows shall be:

So the end result string shall be: "Sml etipetx"

The answer in Java code

Choice 1:

public class Resolution {
    public static String simpleTransposition(String textual content) {
    String proper = "";
    String left = "";
        for (int i=0; i<textual content.size(); i++) {
            if (ipercent2 == 0) left+=textual content.charAt(i);
            else proper+=textual content.charAt(i);
        }
        return left+proper;
    }
}

Choice 2:

public class Resolution {
    public static String simpleTransposition(String textual content) {
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        for(int i = 0; i < textual content.size(); ++i)
          if(i % 2 == 0) sb1.append(textual content.charAt(i)); 
          else sb2.append(textual content.charAt(i)); 
        return sb1.append(sb2).toString();
    }
}

Choice 3:

import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Resolution {
    public static String simpleTransposition(String textual content) {
      String[] letters = textual content.cut up("");
      
      String firstRow = IntStream.vary(0, letters.size)
                .filter(i -> i % 2 == 0)
                .mapToObj(i -> letters[i])
                .gather(Collectors.becoming a member of());
      String secondRow = IntStream.vary(0, letters.size)
                .filter(i -> i % 2 != 0)
                .mapToObj(i -> letters[i])
                .gather(Collectors.becoming a member of());

      return firstRow + secondRow;
    }
}

Check instances to validate our answer

import org.junit.Check;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class SampleTest {
    @Check
    public void basicTest() {
        doTest("Pattern textual content", "Sml etapetx");
        doTest("Easy transposition", "Sml rnpstoipetasoiin");
        doTest("All that glitters will not be gold", "Alta ltesi o odl htgitr sntgl");
        doTest("The higher a part of valor is discretion", "Tebte ato ao sdsrtoh etrpr fvlri icein");
        doTest("Conscience does make cowards of us all", "Cncec osmk oad fu losinede aecwrso sal");
        doTest("Creativeness is extra necessary than information", "Iaiaini oeipratta nwegmgnto smr motn hnkolde");
    }
    personal void doTest(String textual content, String anticipated) {
        assertEquals(anticipated, Resolution.simpleTransposition(textual content));
    }
}
Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here