id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

orion.a.smith@gmail.com apcsa-encryption > apcsaEncryptSumModulus
prev  |  next  |  chance

Given an input String str and a numeric key n, encrypt str using a sum then modulus algorithm, which works as follows.


1) Convert all characters in the input to uppercase - the output will only have uppercase letters but can also have non-letter characters.

2) Each letter is assigned a number: A=0, B=1, ... Z=25.  Anything that isn't a letter (hint: Character.isLetter()) is repeated in the output, in the same position it was in the input.

3) Sum the values of the first n non-space characters together, then calculate the remainder after dividing that sum by 26.  (If there are non-letter characters, remember that the indexes to use for this sum may be more than 0, 1, ..., (n-1).

4) That remainder becomes the basis for the letter of the output, with the same number-to-letter mapping as step 2.

5) The second character of output is formed from a similar algorithm based on characters 2, 3, ..., (n+1) (indexes 1, 2, ..., (n), or ending after n if spaces are present) of the input.

6) When you get to the last characters of the input, "wrap" to the beginning characters.  For example: using an input String with 10 characters that are all letters and n=5, the output at index 8 (where index 9 is the last character) is formed from indexes 8, 9, 0, 1, 2 of the input.  Hint: remainder makes your life easier here, too!

Preconditions: the input will have at least n characters, and n>0.

apcsaEncryptSumModulus("ABAAYZ", 2) → "BBAYXZ"
apcsaEncryptSumModulus("Speak softly and carry a big stick", 12) → "IDRPP WVFAIF NFL QQABZ F FOY GTTWS"
apcsaEncryptSumModulus("The real danger is from you, not from me!", 3) → "ECZ VPOO QTXBDR FO KRYY GVV, UMP KRMC JE!"

...Save, Compile, Run (ctrl-enter)

public String apcsaEncryptSumModulus(String str, int n) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 330

Copyright Nick Parlante 2017 - privacy