In: Computer Science
In very simple cryptograph, the intended message sometimes consists of the first letterof very word in upper case in a string. Write a function cryptmessage that will receive a string or a character vector with the message and return the encrypted message.
>>messagestring=' the Early songbird tweets ';
>>m=cryptmessage(messagestring)
m=' TEST'
Please put it in MATLAB
The code for solving the above problem is as follows:
function result = cryptmessage(input_string)
result = '';
x = strsplit(strtrim(input_string));
for i=1:length(x)
data = string(x(i));
result = append(result, data{1}(1));
end
result = upper(result);
end
An initial empty string result is initialised to store the result of the cryptanalysis. Then, the leading and trailing whitespaces are removed using the strtrim function, and then split into the constituent words using the strsplit function. Then, iterating over the elements of the array of split words, the data is converted into a string from the cell array (since strsplit returns a cell array), and the first character of that string is selected using the indexing method of data{1}(1). This indexed first character is then added to the result string. Finally, the result string is converted into uppercase using the upper function.
The output is as shown below:
A screenshot of the code:
(If you liked the answer, feel free to give it a thumbs up. Thank you so much!)