fix: Caesar cipher produces non-alphabetic output for negative shifts#7508
Merged
DenizAltunkapan merged 2 commits intoJul 3, 2026
Merged
Conversation
normalizeShift cast a possibly-negative int directly to char, which
wraps to a huge unsigned 16-bit value (e.g. (char) -1 == 65535). The
subsequent char arithmetic then overflowed and emitted characters
outside the Latin alphabet, e.g. encode("A", -1) returned '@' instead
of 'Z', and the encode/decode round-trip was broken for negative
shifts.
Normalize the shift into the 0..25 range with ((shift % 26) + 26) % 26
and perform the character arithmetic in int, casting back to char only
when appending. Add regression tests covering negative shifts and the
encode/decode round-trip.
Fixes TheAlgorithms#7506
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7508 +/- ##
============================================
+ Coverage 80.23% 80.25% +0.01%
- Complexity 7355 7358 +3
============================================
Files 810 810
Lines 23785 23787 +2
Branches 4678 4678
============================================
+ Hits 19085 19091 +6
Misses 3940 3940
+ Partials 760 756 -4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
DenizAltunkapan
approved these changes
Jul 3, 2026
DenizAltunkapan
left a comment
Member
There was a problem hiding this comment.
@herley-shaori thanks!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #7506
Problem
Caesar.encode/Caesar.decodeproduced corrupted, non-alphabetic output for any negative shift that is not an exact multiple of 26:Root cause:
normalizeShiftdid(char) (shift % 26). Java's%preserves the dividend's sign, so-1 % 26 == -1, and casting a negativeintto the unsigned 16-bitchartype wraps it to65535. The following char arithmetic then overflowed and truncated into an arbitrary wrong character, which the> 'Z'/< 'A'wraparound checks could not catch. This also broke thedecode(encode(x, s), s) == xround-trip for negative shifts.Fix
normalizeShiftnow returns anintnormalized into0..25via((shift % 26) + 26) % 26, so a negative shift is mapped to its equivalent positive rotation (e.g.-1→25).encode/decodeis performed inintand cast back tocharonly when appending, which also removes thelossy-conversionscompound-assignment pattern.Tests
Added three regression tests to
CaesarTest:caesarEncryptWithNegativeShiftTest—encode("A", -1)must wrap to"Z"(upper and lower case).caesarDecryptWithNegativeShiftTest— decoding with a negative shift.caesarNegativeShiftRoundTripTest— encode→decode round-trip for shifts-1, -5, -25, -26, -27, -52.Verified locally:
mvn test -Dtest=CaesarTest→ 6/6 pass,mvn checkstyle:checkclean,clang-format --dry-run --Werrorclean. All existing tests still pass unchanged.