In: Computer Science
To begin, write a program to loop through a string character by character. If the character is a letter, print a question mark, otherwise print the character. Use the code below for the message string. This will be the first string that you will decode. Use the String class method .charAt(index) and the Character class method .isLetter(char). (You can cut and paste this line into your program.) String msg = "FIG PKWC OIE GJJCDVKLC MCVDFJEHIY BIDRHYO.\n"; String decryptKey = "QWERTYUIOPASDFGHJKLZXCVBNM"; Next modify your program to use this decryption key to decode uppercase letters. For each letter you will find and print the corresponding letter in the decryption key. You will need to use simple character math. Recall that characters are stored inside the computers as numbers: A=65, B=66, C=67, etc. It is considered poor programming to use these numbers directly. Instead use character literal such as 'A' with the understanding that it corresponds to a number. If c is a character, the expression (c - 'A') will calculate an integer value of: 0 when c is 'A', 1 when c is 'B', 2 when c is 'C', etc. This value can then be used as an index to get the corresponding character at that position in the decryption key.
Short Summary:
**************Please do upvote to appreciate our time. Thank you!******************
Source Code:
//This class handles string operations to decrypt
public class DecrypterExample {
//This method is used to find if the string
contains letters
static void isCharacter(String msg)
{
//Loop for string length
for(int
i=0;i<msg.length();i++)
{
if(Character.isLetter(msg.charAt(i)))
{
System.out.print("?");
}
else
System.out.print(msg.charAt(i));
}
}
//This method is used to decrypt the given message
using the decrytp key
static void decryptString(String msg)
{
//Declare a letter array
char[] letters=
{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
//Decrypt key
String decryptKey =
"QWERTYUIOPASDFGHJKLZXCVBNM";
//Loop for string length
for(int
i=0;i<msg.length();i++)
{
char
c=msg.charAt(i);
if(Character.isLetter(c))
{
//Loop the letters and compare with the
character
for(int j=0;j<26;j++)
{
if(c==letters[j])
{
//Break
this loop if character matches
System.out.print(decryptKey.charAt(j));
break;
}
}
}
}
}
//Main function to test the above methods
public static void main(String[] args) {
//Input
String msg = "FIG PKWC OIE
GJJCDVKLC MCVDFJEHIY BIDRHYO.\n";
//To check for characters
isCharacter(msg);
System.out.println();
//To get decrypted text
decryptString(msg);
}
}
Code Screenshot:
Output:
**************Please do upvote to appreciate our time.
Thank you!******************