Thursday, February 29, 2024
HomeSoftware EngineeringThe right way to Encrypt Phrases in Java

The right way to Encrypt Phrases in Java


The problem

You need to create secret messages which have to be deciphered.

Listed here are the circumstances:

  1. Your message is a string containing house separated phrases.
  2. It’s worthwhile to encrypt every phrase within the message utilizing the next guidelines:
    • The primary letter have to be transformed to its ASCII code.
    • The second letter have to be switched with the final letter
  3. Keepin’ it easy: There are not any particular characters within the enter.

Examples:

EncryptWords.encryptThis("Hey") => "72olle"
EncryptWords.encryptThis("good") => "103doo"
EncryptWords.encryptThis("hey world") => "104olle 119drlo"

The answer in Java code

Choice 1:

import java.util.Arrays;
import java.util.Iterator;

public class EncryptWords {
    public static String encryptThis(String textual content) {
        if (textual content.size()==0) return "";

        String[] phrases = textual content.cut up(" ");

        StringBuilder sb = new StringBuilder();

        for (Iterator<String> it = Arrays.stream(phrases).iterator(); it.hasNext(); ) {
            String phrase = it.subsequent();
            StringBuilder nw = new StringBuilder();

            //first char
            nw.append((int)phrase.charAt(0));

            if (phrase.size()>1) {
                //final char
                nw.append(phrase.charAt(phrase.size() - 1));

                //remainder of phrase
                if (phrase.size()>=3) nw.append(phrase.substring(2, phrase.size() - 1));

                //second char
                if (phrase.size()>2) nw.append(phrase.charAt(1));
            }

            //add to fundamental phrase
            sb.append(nw).append(" ");
        }
        return sb.toString().trim();
    }
}

Choice 2:

import java.util.Arrays;
import java.util.stream.Collectors;

public class EncryptWords {
    public static String encryptThis(String textual content) {
        return Arrays.stream(textual content.cut up(" "))
                .map(w->w.size()>2?(int)w.charAt(0)+w.substring(w.size()-1)+w.substring(2, w.size()-1)+w.substring(1,2):
                    w.size()>1?(int)w.charAt(0)+w.substring(1):
                    w.size()==1?(int)w.charAt(0)+"":"")
                .accumulate(Collectors.becoming a member of(" "));
    }
}

Choice 3:

import java.util.Arrays;
import java.util.stream.Collectors;

public class EncryptWords {
    public static String encryptThis(String textual content) {

        return textual content.size() == 0 ? textual content : Arrays.stream(textual content.cut up(" "))
                .map(s -> "" + (int) s.charAt(0)
                        + (s.size() > 2 ? s.charAt(s.size() - 1)
                        + s.substring(2, s.size() - 1)
                        + s.charAt(1) : s.substring(1)))
                .accumulate(Collectors.becoming a member of(" "));
    }
}

Take a look at instances to validate our resolution

import org.junit.jupiter.api.Take a look at;

import static org.junit.jupiter.api.Assertions.*;

public class EncryptWordsTest {
    @Take a look at
    public void exampleTests() {
        assertEquals("", EncryptWords.encryptThis(""));
        assertEquals("65 119esi 111dl 111lw 108dvei 105n 97n 111ka", EncryptWords.encryptThis("A clever outdated owl lived in an oak"));
        assertEquals("84eh 109ero 104e 115wa 116eh 108sse 104e 115eokp", EncryptWords.encryptThis("The extra he noticed the much less he spoke"));
        assertEquals("84eh 108sse 104e 115eokp 116eh 109ero 104e 104dare", EncryptWords.encryptThis("The much less he spoke the extra he heard"));
        assertEquals("87yh 99na 119e 110to 97ll 98e 108eki 116tah 119esi 111dl 98dri", EncryptWords.encryptThis("Why can we not all be like that clever outdated chicken"));
        assertEquals("84kanh 121uo 80roti 102ro 97ll 121ruo 104ple", EncryptWords.encryptThis("Thanks Piotr for all of your assist"));
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments