Wednesday, February 21, 2024
HomeSoftware EngineeringFixing Easy Transposition in Java

Fixing Easy Transposition in Java


The problem

Easy transposition is a primary 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 tip. 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 straightforward transposition.

Instance:

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

So the end result string will probably be: "Sml etipetx"

The answer in Java code

Choice 1:

public class Answer {
    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 Answer {
    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 Answer {
    public static String simpleTransposition(String textual content) {
      String[] letters = textual content.break up("");
      
      String firstRow = IntStream.vary(0, letters.size)
                .filter(i -> i % 2 == 0)
                .mapToObj(i -> letters[i])
                .accumulate(Collectors.becoming a member of());
      String secondRow = IntStream.vary(0, letters.size)
                .filter(i -> i % 2 != 0)
                .mapToObj(i -> letters[i])
                .accumulate(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 shouldn't 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 vital than information", "Iaiaini oeipratta nwegmgnto smr motn hnkolde");
    }
    non-public void doTest(String textual content, String anticipated) {
        assertEquals(anticipated, Answer.simpleTransposition(textual content));
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments