Question

In: Computer Science

To begin, write a program to loop through a string character by character. If the character...

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.

Solutions

Expert Solution

Short Summary:

  1. Implemented the program as per requirement
  2. Attached source code and sample output
  3. Included the two requirements as two methods.

**************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!******************


Related Solutions

Write a program that uses a for loop output a string in reverse printing it character...
Write a program that uses a for loop output a string in reverse printing it character by character. C++ Program Example: "Enter some text:" this is a test "your text in reverse is:" tset a si siht Please note that you can find the length of the string by using the length function: string str = "hello" cout << str.length(); Also you can et a character in a string by providing an index to the function string str = "hello"...
Write a C program that will read a character string and then encrypt the string based...
Write a C program that will read a character string and then encrypt the string based on one of the 3 different encryption methods. The type of encryption is to be selected by the user. Encryption method 1: Swapping by position. Characters in the array are swapped with the opposite characters based on their position in the string. Example: Input string – apple. Encrypted string – elppa Method: The first character ‘a’ and the last character ‘e’ – swap their...
C++, Write a function, noPunct, that would take a string, go through the string character by...
C++, Write a function, noPunct, that would take a string, go through the string character by character, and push any character that is NOT a punctuation mark onto a stack but count the punctuation dropped. After going through the entire string, print the contents of the stack. Then print the number of punctuation dropped. Hint: use ispunct() library function (returns true if character is punctuation)
Write a program that accepts a string and character as input, then counts and displays the...
Write a program that accepts a string and character as input, then counts and displays the number of times that character appears (in upper- or lowercase) in the string. Use C++ Enter a string: mallet Enter a character: a "A" appears 1 time(s) Enter a string: Racecar Enter a character: R "R" appears 2 time(s)
Write a Python loop that goes through the list and prints each string where the string...
Write a Python loop that goes through the list and prints each string where the string length is three or more and the first and last characters of the strings are the same. Test your code on the following three versions of the list examples: examples = ['abab', 'xyz', 'aa', 'x', 'bcb'] examples = ['', 'x', 'xy', 'xyx', 'xx'] examples = ['aaa', 'be', 'abc', 'hello'].
Write a program whose input is a string which contains a character and a phrase, and...
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. Ex: If the input is: n Nobody the output is: 0...
Write a basic C++ program with function, whose input is a character and a string, and...
Write a basic C++ program with function, whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex: If the input is: n...
In objective-C Task: Write a program whose input is a character and a string, and whose...
In objective-C Task: Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.You may assume that the string does not contain spaces and will always contain less than 50 characters. Ex: If the input is: n Monday the output is:...
Using python: Given a string x, write a program to check if its first character is...
Using python: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.
# PYTHONWrite a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.Ex: If the input is:n Mondaythe output is:1Ex: If the input is:z Today is Mondaythe output is:0Ex: If the input is:n It's a sunny daythe output is:2Case matters.Ex: If the input is:n Nobodythe output is:0n is different than N.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT