Question

Write a cencrypt() function that takes a arbitrary length string and an integer as inputs and...

Write a cencrypt() function that takes a arbitrary length string and an integer as inputs and returns a string as output.

The cencrypt() function you write in lab today needs to work on strings that contain upper-case letters, lower-case letters, or both. The output string should be all upper-case letters. (The Romans of Caesar's time used only what today we call upper-case letters.)

So a call to cencrypt("Caesar", 1) should return (return not print) the string “DBFTBS” and a call to cencrypt(“DBFTBS”, -1)should return the string "CAESAR".

The string function .upper() mentioned in the assigned reading in Chapter 7.3 of Zybooks that gives back a version of its string that has all alphabet characters converted to upper case may be useful here.

Homework Answers

Answer #1

CODE IN JAVA:

Cencrypt.java file:

import java.util.Arrays;

public class Cencrypt {
   public static String cencrypt(String input,int n) {
       input = input.toUpperCase();
       int len = input.length();
       int temp;
       char arr[]=new char[len];
       for(int i=0;i<len;i++) {
           temp = charToInt(input.charAt(i))+n;
           if(temp>26)
               temp = temp - 26;
           else if(temp<1)
               temp = temp + 26;
           arr[i] = intToChar(temp);
       }
       return Arrays.toString(arr)
       .substring(1, 3*arr.length-1)
       .replaceAll(", ", "");
   }
   //method to get corresponding character
   static char intToChar(int n) {
       char alpha[] = {'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'};
       return alpha[n-1];
   }
   //method to get corresponding integer
   static int charToInt(char ch) {
       char alpha[] = {'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'};
       for(int i=0;i<alpha.length;i++) {
           if(alpha[i]==ch) {
               return i+1;
           }
       }
       return 0;
   }
   public static void main(String[] args) {
       String input = "Caesar";
       System.out.println("The input string is:"+input);
       //calling the methods and displaying the result
       String output = cencrypt(input,1);
       System.out.println("After calling the cencrypt method with n=1 the output is:"+output);
       String temp = cencrypt(output,-1);
       System.out.println("After calling the cencrypt method with n=-1 the output is:"+temp);
   }

}
OUTPUT:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
3. Write function, leastChar(inputString) that takes as input a string of one or more letters (and...
3. Write function, leastChar(inputString) that takes as input a string of one or more letters (and no other characters) and prints 1) the "least" character in the string, where one character is less than another if it occurs earlier in the alphabet (thus 'a' is less than 'C' and both are less than 'y') and 2) the index of the first occurrence of that character. When comparing letters in this problem, ignore case - i.e. 'e' and 'E' should be...
Write a Java application with a JavaFX GUI that takes a String input by the user...
Write a Java application with a JavaFX GUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum,...
Write a Java application a String input by the user and shows whether the string contains...
Write a Java application a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. needs, at minimum, asl user to input for the String, then...
2. Define a function write_to_file(filename, text) that takes in a filename (a string) and a list...
2. Define a function write_to_file(filename, text) that takes in a filename (a string) and a list of strings as arguments. Your function will open a file and write the contents of text onto the file. Each element in the list represents a separate line in the file, without newline characters. You will have to add the newline characters in yourself. This function does not return or print anything; it will create a new file.
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
Python The capitalize function should receive a word as a parameter in the form of a...
Python The capitalize function should receive a word as a parameter in the form of a string and return one version where the first character is written in capital letters. Other characters must be unchanged. For example ‘hey’ become ‘Hey’. Write the function clearly. Consider that str has the method upper that returns a new string where all characters are capitalized
Write a c++ trim function that takes an input of string and output the string by...
Write a c++ trim function that takes an input of string and output the string by trimming away all non-alphabetic letters.
Write a short RISC-V assembly program that operates on a NULL terminated string of arbitrary size...
Write a short RISC-V assembly program that operates on a NULL terminated string of arbitrary size (use your favourite phrase when you define it with DC for testing). The program (no need to define a function) scans the string and replaces every lower case character with the corresponding upper case. Your program prints the string before and after converting it to upper case.
Python Practice Sample: Write a function shuffle (s1, s2) which creates a new string formed by...
Python Practice Sample: Write a function shuffle (s1, s2) which creates a new string formed by “shuffling” the letters of two strings s1 and s2. If the strings are not of equal lengths, concatenate the remaining letters of the longer string to the result. Your program should work with the code below: s1 = "abcd" s2 = "1234" s3 = "abcdefg" s4 = "123456" print(f"{s1:10s}\t{s2:10s}\t{shuffle (s1,s2)}") print(f"{s3:10s}\t{s2:10s}\t{shuffle (s3,s2)}") print(f"{s1:10s}\t{s4:10s}\t{shuffle (s1,s4)}") Output: abcd 1234 a1b2c3d4 abcdefg 1234 a1b2c3d4efg abcd 123456 a1b2c3d456
This is an intro to Python question: #Write a function called linear() that takes two parameters...
This is an intro to Python question: #Write a function called linear() that takes two parameters #- a list of strings and a string. Write this function so #that it returns the first index at which the string is #found within the list if the string is found, or False if #it is not found. You do not need to worry about searching #for the search string inside the individual strings within #the list: for example, linear(["bobby", "fred"], "bob") #should...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT