Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Function Name: caesarShift Inputs 1. 2. (char) A string to be encoded (double) T

ID: 3749226 • Letter: F

Question

Function Name: caesarShift Inputs 1. 2. (char) A string to be encoded (double) The shift number Outputs 1. (char) An encoded string Function Background: You're going to Music Midtown '18! 18 BCE that is, of course! You're a famous artist in Roman times. You want to send the lyrics to be edited, but you don't want the couriers to read it, so you decide to encrypt your lyrics. You remember that your friend, Julius Caesar, had a great method for encoding his messages. Caesar shifted each letter over 3 places, so that 'a" became 'd'. The shift wraps around the end of the alphabet, so the letter 'z' would shift to become the letter "c'. However, the Caesar shift is relatively easy to crack today, so in order to encrypt all of the lyrics, you decide to make it harder to crack by adding some more steps. Being a Roman MATLAB pro, you decide to write a function that does it for you! Function Description: The function takes in a string to be encoded and a shift number representing how far each letter will be shifted for the Caesar shifts you will perform. It then performs the following actions in order on the input string 1. Convert all letters to uppercase, since classical Latin had one case 2. For letters with even ASCIl values, perform a Caesar shift using the given shift number 3. For letters with odd ASCII values, perform a Caesar shift using the negative of the shift numbe 4. Replace all instances of '' with 'I' and 'U' with 'V', since classical Latin had no J's or U's (Julius Caesar was written as IVLIVS CAESAR) 5. Concatenate the number of consonants in the resulting string with the output string including the 'V's added in Step 4 Notes: . . . The function should work for both positive and negative shift values of any magnitude Consonants are defined as letters that are not A, E, I, O, or U (and not spaces) The input string is guaranteed to only consist of letters and spaces. Classical Latin did not use punctuation Hints . The mod () and strrep and num2str() functions and will be useful.

Explanation / Answer

The following function is written in C# code.

Please note that while performing the encoding the 'space' character i.e. ' ', has not been considered as a letter and thereby no encoding has been applied to the 'space' character. However if that is needed, then simply removing the if condition below the line "//If the character is not a 'space' i.e. ' '" will be sufficient to bring the necessary changes.

public static string CaesarShift(string message, int shift)
{
string myMessage="";
int countConsonant = 0;
//Converting the input text to upper case
message = message.ToUpper();
  
foreach (char alphabet in message)
{

int ch = alphabet; //It will implicitely convert a char to corresponding ASCII value
int finalChar = 32; //Initializing the output character with ASCII value of ' ' i.e. 32. if the character is not space' it will be overwritten later


//Check If the character is not a 'space' i.e. ' '
if (ch != 32)
{
//Checking if the ASCII value is even and thereby implementing positive Caesar Shift
if (ch % 2 == 0)
{
finalChar = ch + shift;
}
//If the ASCII value is odd then implementing negative Caesar Shift
else
{
finalChar = ch - shift;
}

//Implementing Shift wrap around the alphabets
while (finalChar < 65)
{
finalChar = 90 - (65 - finalChar) + 1;
}
while (finalChar > 90)
{
finalChar = 65 + (finalChar - 90) - 1;
}

}
//Adding individual character to the output after implementing Condition 1,2 & 3 from the question
myMessage += Convert.ToChar(finalChar);

//Implementing Condition 4 from the question i.e. converting every 'J' to 'I' and 'U' to 'V'
myMessage = myMessage.Replace('U', 'V').Replace('J', 'I');

}

//Counting the number of consonants in the output encoded string
foreach (char character in myMessage)
{
if (character != 'A' && character != 'E' && character != 'I' && character != 'O' && character != 'U' && character != ' ')
{
countConsonant++;
}
}
//Concatinating the number of Consonants with the output string as asked in Condition 5 of the Question
myMessage += Convert.ToString(countConsonant);
return myMessage;
}