Question

In: Computer Science

Given an original message, determine the cipher that will produce the encoded string that comes earliest...

Given an original message, determine the cipher that will produce the encoded string that comes earliest alphabetically. Return this encoded string. In the example below, the second cipher produces the alphabetically earliest encoded string ("abccd").

For example, if John's message is "hello" and his cipher maps 'h' to 'd', 'e' to 'i', 'l' to 'p' and 'o' to 'y', the encoded message will be "dippy". If the cipher maps 'h' to 'a', 'e' to 'b', 'l' to 'c' and 'o' to 'd', then the encoded message will be "abccd".

My code (bolded code is part of the question and cannot be changed):

string encrypt(string message){

// you write code here

string unEncryp;

for(int i = 0; i < message.length()){

char temp = message[i];

switch(temp){

default:

break;

}

}

return unEncryp;

}

Solutions

Expert Solution

Please follow the code and comments for description :

CODE :

import java.util.HashMap; // required imports
import java.util.Map;

public class Cipher // class to run the code
{

public static String encrypt(String message) // method that encrypts the string as desired
{
char charData = 'a'; // character starts
StringBuilder sb = new StringBuilder(); // string builder object
Map<Character, Character> msgData = new HashMap<>(); // map that stores the characters
for (Character c : message.toCharArray()) // iterate over the map
{
if (!msgData.containsKey(c)) // check for the match character
{
sb.append(charData); // append the data
msgData.put(c, charData);
++charData;
} else
{
sb.append(msgData.get(c));
}
}
return sb.toString(); // return the string
}

public static void main(String[] args) // driver method
{
String message = "hello";
System.out.println("The Input String is : " + message);
System.out.println("The Result String is : " + encrypt(message));
}
}


OUTPUT :

Hope this is helpful.


Related Solutions

Stream Cipher. A message is encoded by exclusive-or’ing each bit with a random bit stream. (In...
Stream Cipher. A message is encoded by exclusive-or’ing each bit with a random bit stream. (In exclusive or, 0⊕0 = 0, 1⊕0 = 1, 0⊕1 = 1, and 1⊕1 = 0.) a. Recover the original message from the random stream and encrypted stream, by filling in the blanks in the “Recovered” and “ASCII” rows. (In the ASCII column, interpret the byte as an ASCII character and write the value in that row.) Message: ? ? ? ? ? ? ?...
(a) Use Vigenere cipher to encrypt the message “United States Constitution” using the keyword      “covid” Given...
(a) Use Vigenere cipher to encrypt the message “United States Constitution” using the keyword      “covid” Given that the Vigenere cipher of part (a) with the same keyword was used to produce the ciphertext: VFPUSVSNBVRCNQW Find the plaintext message.        
Determine the CFU/mL in the original suspension of E. coli given that: a) 2300 and 1990...
Determine the CFU/mL in the original suspension of E. coli given that: a) 2300 and 1990 colonies were present when 0.1mL of the 10^-3 dilution was plated in duplicate, b) 218 & 196 colonies were present when 0.1 mL of the 10^-4 dilution was plated in duplicate, and c) 18 & 10 colonies were present when 0.1 mL of the 10^-5 dilution was plated in duplicate.
From the Fe2+ concentrations given determine the mg of Fe in the original vitamin tablet. You...
From the Fe2+ concentrations given determine the mg of Fe in the original vitamin tablet. You will need to convert ug/mL into mg of iron. You also have to consider that the original vitamin tablet sample was diluted to 250mL in a volumetric flask and then 5mL of that sample was diluted to 100mL in a volumetric flask. Please show all equations used and work done. 1. Concentration of 0.141738 2. Concentration of 0.1307058 3. Concentration of 0.1354047
Given the forecasted data, determine the number of planes that the company must produce in order...
Given the forecasted data, determine the number of planes that the company must produce in order to break even, on both accounting basis and NPV basis. The 10-year project initial investment is $1,000 million, each plane sold for $15 million, the variable cost is $7 million each plane, the fixed cost is $150 million, the depreciation uses straight-line method, tax rate is 40% and the company’s cost of capital is 12%. please show work in Excel. Can you also put...
Given the following forecasted data, determine the number of planes that the company must produce in...
Given the following forecasted data, determine the number of planes that the company must produce in order to break even, on both accounting basis and NPV basis: the 6-year project initial investment is $900 million, each plane sold for $15.5 million, the variable cost is $8 million each plane, the fixed cost is $150 million, the depreciation uses straight-line method, tax rate is 30% and the company’s cost of capital is 10%. Please show all work in excel. Thank you!
Given the following forecasted data, determine the number of planes that the company must produce in...
Given the following forecasted data, determine the number of planes that the company must produce in order to break even, on both accounting basis and NPV basis assuming 6years as life of project. The project initial investment is $900 million, each plane sold for $15.5 million, the variable cost is $8 million each plane, the fixed cost is $150 million, the depreciation uses straight-line method, tax rate is 30% and the company’s cost of capital is 10%. Please explain work...
Briefly describe how you would determine whether a given salt will produce an acidic, basic, or...
Briefly describe how you would determine whether a given salt will produce an acidic, basic, or neutral solution when dissolved in water.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT