Repository: TheAlgorithms/Java
Pull request #5649 (2024-10-08 11:30:04)

fix columnarTranspositionCipher and typos in Test
…herTest

<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [ ] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [ ] This pull request is all my own work -- I have not plagiarized it.
- [ ] All filenames are in PascalCase.
- [ ] All functions and variable names follow Java naming conventions.
- [ ] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [ ] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5649?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `71.42857%` with `2 lines` in your changes missing coverage. Please review.
> Project coverage is 58.20%. Comparing base [(`9fb8192`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/9fb819235643b8a6afa392b65e72a46036c4c210?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`4a3109a`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/4a3109a885aebae399a9f8be8b9329ac50055021?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).
> Report is 2 commits behind head on master.

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5649?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...lgorithms/ciphers/ColumnarTranspositionCipher.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5649?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fciphers%2FColumnarTranspositionCipher.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9jaXBoZXJzL0NvbHVtbmFyVHJhbnNwb3NpdGlvbkNpcGhlci5qYXZh) | 71.42% | [0 Missing and 2 partials :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5649?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5649      +/-   ##
============================================
+ Coverage     58.17%   58.20%   +0.02%     
- Complexity     3687     3692       +5     
============================================
  Files           548      549       +1     
  Lines         15832    15850      +18     
  Branches       3012     3015       +3     
============================================
+ Hits           9210     9225      +15     
- Misses         6230     6232       +2     
- Partials        392      393       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5649?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java b/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java
index d7e64a12..b6b889b0 100644
--- a/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java
+++ b/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java
@@ -27,13 +27,13 @@ public final class ColumnarTranspositionCipher {
      * @return a String with the word encrypted by the Columnar Transposition
      * Cipher Rule
      */
-    public static String encrpyter(String word, String keyword) {
+    public static String encrypt(final String word, final String keyword) {
         ColumnarTranspositionCipher.keyword = keyword;
-        abecedariumBuilder(500);
+        abecedariumBuilder();
         table = tableBuilder(word);
         Object[][] sortedTable = sortTable(table);
         StringBuilder wordEncrypted = new StringBuilder();
-        for (int i = 0; i < sortedTable[i].length; i++) {
+        for (int i = 0; i < sortedTable[0].length; i++) {
             for (int j = 1; j < sortedTable.length; j++) {
                 wordEncrypted.append(sortedTable[j][i]);
             }
@@ -51,11 +51,12 @@ public final class ColumnarTranspositionCipher {
      * @return a String with the word encrypted by the Columnar Transposition
      * Cipher Rule
      */
-    public static String encrpyter(String word, String keyword, String abecedarium) {
+    public static String encrypt(String word, String keyword, String abecedarium) {
         ColumnarTranspositionCipher.keyword = keyword;
         ColumnarTranspositionCipher.abecedarium = Objects.requireNonNullElse(abecedarium, ABECEDARIUM);
         table = tableBuilder(word);
         Object[][] sortedTable = sortTable(table);
+
         StringBuilder wordEncrypted = new StringBuilder();
         for (int i = 0; i < sortedTable[0].length; i++) {
             for (int j = 1; j < sortedTable.length; j++) {
@@ -72,7 +73,7 @@ public final class ColumnarTranspositionCipher {
      * @return a String decrypted with the word encrypted by the Columnar
      * Transposition Cipher Rule
      */
-    public static String decrypter() {
+    public static String decrypt() {
         StringBuilder wordDecrypted = new StringBuilder();
         for (int i = 1; i < table.length; i++) {
             for (Object item : table[i]) {
@@ -91,14 +92,14 @@ public final class ColumnarTranspositionCipher {
      */
     private static Object[][] tableBuilder(String word) {
         Object[][] table = new Object[numberOfRows(word) + 1][keyword.length()];
-        char[] wordInChards = word.toCharArray();
-        // Fils in the respective numbers
+        char[] wordInChars = word.toCharArray();
+        // Fills in the respective numbers for the column
         table[0] = findElements();
         int charElement = 0;
         for (int i = 1; i < table.length; i++) {
             for (int j = 0; j < table[i].length; j++) {
-                if (charElement < wordInChards.length) {
-                    table[i][j] = wordInChards[charElement];
+                if (charElement < wordInChars.length) {
+                    table[i][j] = wordInChars[charElement];
                     charElement++;
                 } else {
                     table[i][j] = ENCRYPTION_FIELD_CHAR;
@@ -116,7 +117,7 @@ public final class ColumnarTranspositionCipher {
      * order to respect the Columnar Transposition Cipher Rule.
      */
     private static int numberOfRows(String word) {
-        if (word.length() / keyword.length() > word.length() / keyword.length()) {
+        if (word.length() % keyword.length() != 0) {
             return (word.length() / keyword.length()) + 1;
         } else {
             return word.length() / keyword.length();
@@ -173,13 +174,11 @@ public final class ColumnarTranspositionCipher {
     }
 
     /**
-     * Creates an abecedarium with a specified ascii inded
-     *
-     * @param value Number of characters being used based on the ASCII Table
+     * Creates an abecedarium with all available ascii values.
      */
-    private static void abecedariumBuilder(int value) {
+    private static void abecedariumBuilder() {
         StringBuilder t = new StringBuilder();
-        for (int i = 0; i < value; i++) {
+        for (int i = 0; i < 256; i++) {
             t.append((char) i);
         }
         abecedarium = t.toString();


Test patch:
diff --git a/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java b/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java
index 0d306a66..8deae450 100644
--- a/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java
+++ b/src/test/java/com/thealgorithms/ciphers/ColumnarTranspositionCipherTest.java
@@ -20,7 +20,7 @@ public class ColumnarTranspositionCipherTest {
 
     @Test
     public void testEncryption() {
-        String encryptedText = ColumnarTranspositionCipher.encrpyter(plaintext, keyword);
+        String encryptedText = ColumnarTranspositionCipher.encrypt(plaintext, keyword);
         assertNotNull(encryptedText, "The encrypted text should not be null.");
         assertFalse(encryptedText.isEmpty(), "The encrypted text should not be empty.");
         // Check if the encrypted text is different from the plaintext
@@ -29,19 +29,19 @@ public class ColumnarTranspositionCipherTest {
 
     @Test
     public void testDecryption() {
-        String encryptedText = ColumnarTranspositionCipher.encrpyter(plaintext, keyword);
-        String decryptedText = ColumnarTranspositionCipher.decrypter();
+        String encryptedText = ColumnarTranspositionCipher.encrypt(plaintext, keyword);
+        String decryptedText = ColumnarTranspositionCipher.decrypt();
 
         assertEquals(plaintext.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original plaintext, ignoring spaces.");
-        assertEquals(encryptedText, ColumnarTranspositionCipher.encrpyter(plaintext, keyword), "The encrypted text should be the same when encrypted again.");
+        assertEquals(encryptedText, ColumnarTranspositionCipher.encrypt(plaintext, keyword), "The encrypted text should be the same when encrypted again.");
     }
 
     @Test
     public void testLongPlainText() {
         String longText = "This is a significantly longer piece of text to test the encryption and decryption capabilities of the Columnar Transposition Cipher. It should handle long strings gracefully.";
-        String encryptedText = ColumnarTranspositionCipher.encrpyter(longText, keyword);
-        String decryptedText = ColumnarTranspositionCipher.decrypter();
+        String encryptedText = ColumnarTranspositionCipher.encrypt(longText, keyword);
+        String decryptedText = ColumnarTranspositionCipher.decrypt();
         assertEquals(longText.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original long plaintext, ignoring spaces.");
-        assertEquals(encryptedText, ColumnarTranspositionCipher.encrpyter(longText, keyword), "The encrypted text should be the same when encrypted again.");
+        assertEquals(encryptedText, ColumnarTranspositionCipher.encrypt(longText, keyword), "The encrypted text should be the same when encrypted again.");
     }
 }