0
#include <stdio.h>
#include <ctype.h>
#include <cs50.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main(int count, string input[])
{
    // array of small alphabets
    char smaAlph[26] = {
        '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'
    };

// capital aphabets
char capAlph[26] = {
    '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'
};

int key;
if (count == 2)
{
    int digit, n;

    // assigning the string length
    n = strlen(input[1]);

    for (int i = 0; i < n; i++)
    {
        if (input[1][i] < '0' || input[1][i] > '9')
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }

    // getting the key
    key = atoi(input[1]);

    // trying encryption
    //getting normal text
    string text = get_string("plaintext: ");
    int n2 = strlen(text);


    for (int i = 0; i < n2; i++)
    {
        // testing if the char alphabet
        if (isalpha(text[i]))
        {
            // testing if the char upper text
            if (isupper(text[i]))
            {
                // looping through the cap alphabet array
                for (int p = 0; p < 26; p++)
                {
                    // testing if the char matches the char in the array
                    // switcing from ASCII index to alphabet index
                    if (capAlph[p] == text[i])
                    {
                        // applying the formula & returning to ascii
                        text[i] = ((capAlph[p] + key) % 26) + 'A';
                    }
                }
            }
            // testing if the char lower text
            if (islower(text[i]))
            {
                for (int p = 0; p < 26; p++)
                {
                    // looping through the cap alphabet array
                    // doing same as the CAP alphabet
                    if (smaAlph[p] == text[i])
                    {
                        // switching to smaAlph index and ascii
                        text[i] = ((smaAlph[p] + key) % 26) + 'a';
                    }
                }
            }
        }
    }
    printf("ciphertext: %s\n", text);

    return 0;

}
else
{
    printf("Usage: ./caesar key\n");
    return 2;
}
}

my code is compiling correctly but it doesn't function correctly (doesn't print the expected output) That is required in the assessment. It should cypher the input text by shifting the input alphabets (and only alphabets) by the value specified in the key input.

Edit

#include <stdio.h>
#include <ctype.h>
#include <cs50.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main(int count, string input[])
{
    // array of small alphabets
    char smaAlph[26] = {
        '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'
    };

 



// capital aphabets
    char capAlph[26] = {
        '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'
    };

int key;
if (count == 2)
{
    int digit, n;

    // assigning the string length
    n = strlen(input[1]);

    for (int i = 0; i < n; i++)
    {
        if (input[1][i] < '0' || input[1][i] > '9')
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }

    // getting the key
    key = atoi(input[1]);

    // trying encryption
    //getting normal text
    string text = get_string("plaintext: ");
    int n2 = strlen(text);


    for (int i = 0; i < n2; i++)
    {
        // testing if the char alphabet
        if (isalpha(text[i]))
        {
            // testing if the char upper text
            if (isupper(text[i]))
            {
                // looping through the cap alphabet array
                for (int p = 0; p < 26; p++)
                {
                    // testing if the char matches the char in the array
                    // switcing from ASCII index to alphabet index
                    if (capAlph[p] == text[i])
                    {
                        // applying the formula & returning to ascii
                        text[i] = ((capAlph[p] + key) % 26);
                        
                        // returning to ascii
                        text[i] += 'A';
                    }
                }
            }
            // testing if the char lower text
            if (islower(text[i]))
            {
                for (int p = 0; p < 26; p++)
                {
                    // looping through the cap alphabet array
                    // doing same as the CAP alphabet
                    if (smaAlph[p] == text[i])
                    {
                        // switching to smaAlph index and ascii
                        text[i] = ((smaAlph[p] + key) % 26);
                        
                        // returning to ascii
                        text[i] += 'a';
                    }
                }
            }
        }
    }
    printf("ciphertext: %s\n", text);

    return 0;

}
else
{
    printf("Usage: ./caesar key\n");
    return 2;
}
}
5
  • The code posted above compiles correctly. Did you save the source code after changing it? Did you recompile it? Did you recompile the same source code file as you saved? It does compile, but it doesn't run correctly. That's a different issue. Please edit your question after you've investigated this issue.
    – Cliff B
    Jun 5, 2021 at 22:41
  • Sorry, I didn't figure out this. I 've edited the question to explicitly include the issue, Thanks. Jun 6, 2021 at 3:22
  • Two hints. 1. comparing the letter to be encoded to a list of letters serves no purpose and that code can be deleted. 2. The first step is to convert the letter's ASCII value to a number from 0 to 25. Perhaps you should rewatch the related videos that explain this.
    – Cliff B
    Jun 6, 2021 at 23:21
  • Thanks for your help, I 'll give it a try. Jun 7, 2021 at 9:47
  • Problem solved, it was in the switching between alphabetical index and ascii index. Thanks. Jun 7, 2021 at 15:20

1 Answer 1

1

Simply put, the logic of encoding the letters is wrong. The logic of the current code is to take the letter from the input string, find the same matching letter in an array (ie, replace q with q, which does nothing), add the key to that ASCII value and apply %26. Finally, applying %26 directly to an ASCII value doesn't produce a valid result.

The code needs to first convert an ASCII value to a number from 0 to 25 inclusive. Then add the key and apply %26 to that result. Finally, convert the number (0 to 25 inclusive) back to an ASCII value.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • I tried to make all this in a single step in the inner-most condition, but I edited the code by splitting it into 2 steps because it didn't seem to be logic, but it still doesn't work correctly. Jun 6, 2021 at 5:01
  • Please help. :) Jun 6, 2021 at 11:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .