Question

In: Computer Science

1. Maintain two arrays for use in “translating” regular text to “leetspeak”: (a) One array will...

1. Maintain two arrays for use in “translating” regular text to “leetspeak”:
(a) One array will contain the standard characters:
char[] letters = {'a', 'e', 'l', 'o', 's', 't'}; (b) The other will contain the corresponding symbols:
char[] symbols = {'@', '3', '1', '0', '$', '7'};
2. Prompt the user to enter a full sentence with all lower-case characters, then store it in a variable to be
used later.
(a) You can (optionally) manually convert the string to all lower-case with the String method called
toLowerCase(), for example:
String one = "Hello, World!";
String two = one.toLowerCase(); System.out.println(two); // Output: hello, world!
3. Convert the input string to an array of characters.
4. Then, use a loop to iterate the array of characters and, for each one, check to see if it is in the letters
array: if it is, replace with the corresponding element (i.e. same index) in the symbols array. (a) For example, if the user inputs leetspeak the output should be 1337$p3@k.
2

5. Next,“reset”thecharacterarraytomatchtheoriginalinputstring–youcanjustusethe.toCharArray() command again.
6. Now, prompt the user for a positive integer, store it in a variable. This will be a shift for a basic “Caesar Cipher”.
7. Finally, loop the character array and, if the character is not a space, display: (a) Show the character itself
(b) Show the corresponding ASCII code by converting it to an int. See the following example code of type casting between int and char:
char myChar = 'd';
int myInt = 5;
int combined = myChar + myInt;
System.out.println(myChar); // d
System.out.println((int) myChar); // 100
// NB: when doing addition, they become an int if you don't specify System.out.println(myChar + myInt) // 105
System.out.println((char) combined); // i
(c) Show the result of adding the user’s integer to the ASCII code and converting back to a char.
i. When you add, you want to “wrap around” from z back to a. So to accommodate this, you can use the modulus operator, % along with some clever arithmetic.
ii. For example, if the user enters 5 for the shift value, and you have the character x (ASCII value 120), the result should be c (ASCII value 99); likewise, if the character is b (ASCII value 97) the result would be g (ASCII value 103).
8. Either with a new for-loop, or in the one above, change the elements of the character array by adding the integer to each one. Then display the “shifted” sentence to the user. This is known as the “Caesar Cipher”, and was an early form of sending encrypted messages; you just need to know what the shift value was in the beginning, then subtract it out from the message you receive!


Java

Solutions

Expert Solution

import java.util.Scanner;
class Main
{
        public static void main (String[] args)
        {
            // Part 1
            char[] letters = {'a', 'e', 'l', 'o', 's', 't'}; 
        char[] symbols = {'@', '3', '1', '0', '$', '7'};
        
        // Part 2
                Scanner sc = new Scanner(System.in);
                System.out.println("Enter sentence in lower case");
                String str = sc.nextLine();
                str = str.toLowerCase(); 
                
                // Part 3
                char arr[] = str.toCharArray();
                
                // Part 4
                String new_str = "";
                int flag=0;
                for(int i=0;i<arr.length;i++){
                    flag=0;
                    for(int j=0;j<letters.length;j++){
                        if(arr[i]==letters[j]){
                            new_str+=symbols[j];
                            flag=1;
                            break;
                        }
                    }
                    if(flag==0)
                        new_str+=arr[i];
                }
        System.out.println(new_str);
        
        // Part 5
        char array[] = str.toCharArray();
        
        // Part 6
        System.out.print("Enter a number for shifting the text: ");
        int shift = sc.nextInt();
        
        // Part 7
    char tempstr;
    String finalstr="";
        for(int i=0;i<array.length;i++){
            if(array[i]!= ' '){
                int combined = array[i] + shift;
                int element = combined-97;
                element = element%26;
                combined = element+97;
            System.out.println(array[i]);
            System.out.println(combined);
            System.out.println((char) combined);
            tempstr = (char)combined;
            }
            else
                tempstr = ' ';
           finalstr += tempstr;   
        }
        
        // Part 8
        System.out.println(finalstr);
        
        }
}


NOTE: The above code is in Java. Please refer to the attached screenshots for code indentation and sample I/O.

SAMPLE I/O:


Related Solutions

Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Define problem recognition. How is this process like translating text from one language into another? What...
Define problem recognition. How is this process like translating text from one language into another? What role does “probing” play in this process?
Description: One problem with dynamic arrays is that once the array is created using the new...
Description: One problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This assignment asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have the following: • A private member variable called...
3. Array Operations 3-1 Write a function, equalsArray that when passed two int arrays of the...
3. Array Operations 3-1 Write a function, equalsArray that when passed two int arrays of the same length that is greater than 0 will return true if every number in the first array is equal to the number at the same index in the second array. If the length of the arrays is less than 1 the function must return false. For example, comparing the two arrays, {1,2,3,4,5} and {1,2,3,4,5} would return true but the two arrays {3,7} and {3,6}...
1. With respect to resizable arrays, establish the relationship between the size of the array and...
1. With respect to resizable arrays, establish the relationship between the size of the array and the time it takes to perform random access (read a the value stored at a given position somewhere in the array). Provide empirical evidence to support your answer.
Your task is to modify the program from the Java Arrays programming assignment to use text...
Your task is to modify the program from the Java Arrays programming assignment to use text files for input and output. I suggest you save acopy of the original before modifying the software. Your modified program should: contain a for loop to read the five test score into the array from a text data file. You will need to create and save a data file for the program to use. It should have one test score on each line of...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 2: Find the largest value of each row of a 2D array             (filename: FindLargestValues.java) Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 1: Find the average of an array of numbers (filename: FindAverage.java) Write two overloaded methods with the following headers to find the average of an array of integer values and an array of double values: public static double average(int[] num) public static double average(double[] num) In the main method, first create an...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the kth smallest item in an array. Use the first item as the pivot. Compare sets of results using a static call counter. Reset counter before running another search. Create a Test drive to exhaustively test the program. // Assume all values in S are unique. kSmall(int [] S, int k): int (value of k-smallest element) pivot = arbitrary element from S:  let’s use the first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT