0

It has been more then a week and I am unable to fix this problem. I double checked everything but I still get this error with capital letters. Can anyone help please?

Here is the code I have for this:

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

int changeTextToASCIInbr(int x);
void getCipherText(int x);
int changeCAPSToASCIInbr(int x);
void getCapsCipherText(int x);

int main(int argc, string argv[])
{
    if (argc == 2)
    {
        int k = atoi(argv[1]);
        string p = GetString();
        for (int i = 0, n = strlen(p); i < n; i++)
        {
            if(isdigit(p[i]))
            {
                printf("%c", p[i]);
            }
            else
            {
                if (isupper(p[i]))
                {
                    int capsP = changeCAPSToASCIInbr(p[i]);
                    int capsCipherText = (capsP + k) % 26;
                    getCapsCipherText(capsCipherText);
                }
                else if (isspace(p[i]))
                {
                    printf(" ");
                }
                else if (ispunct(p[i]))
                {
                    printf("%c", p[i]);
                }
                else if (islower(p[i]))
                {
                    int ptext = changeTextToASCIInbr(p[i]);
                    int cipherText = (ptext + k) % 26;
                    getCipherText(cipherText);
                }
            }
        }
        printf("\n");
        return 0;
    }
    else
    {
        printf("Usage: /home/ubuntu/workspace/week2/pset2/caesar <key>\n");
        return 1;
    }
}

//changes the char to integer and integer is between 0 and 26. so it changes             ascii keys as well.
int changeTextToASCIInbr(int x)
{
    int changeIncoming;
    changeIncoming = x - 97;
    return changeIncoming;
}

//changes the give number (betwen 0 and 26) to its corresponding ascii key and then converts to char.
void getCipherText(int x)
{
    int changeIncoming;
    changeIncoming = x + 97;
    int asc[26];
    for (int i = 97; i < 122; i++)
    {
        asc[i] = i;
    }
    printf("%c", asc[changeIncoming]);
}

//two bellow functions does the same as above but for capital letters
int changeCAPSToASCIInbr(int x)
{
    int changeIncoming;
    changeIncoming = x - 65;
    return changeIncoming;
}

void getCapsCipherText(int x)
{
    int changeIncoming;
    changeIncoming = x + 65;
    int asc[26];
    for (int i = 65; i < 91; i++)
    {
        asc[i] = i;
    }
    printf("%c", asc[changeIncoming]);
}
#

enter image description here

2
  • Without seeing any code, it could be anything.
    – Cliff B
    May 10 '16 at 20:16
  • ohh that is so right, I just pasted the code here, thanks
    – maysam
    May 11 '16 at 21:23
0

Your faultalmost certainly has something to do with the uppercase section of your code.

Certainly something more succinct could be implemented but check each of your three lines to determine their output for capital letters.

Can you post a encryption of "ABCDEFGHI"with "a" and post the result?

In terms of good practice:

For readability, this function and the very similar one for capital letters is a waste of space and memory and makes your code more difficult to read.

   int changeTextToASCIInbr(int x)
    {
        int changeIncoming;
        changeIncoming = x - 97;
        return changeIncoming;
    }

replace this and the above function

int ptext = changeTextToASCIInbr(p[i]);

with

int ptext = (p[i] - 97);

Try and keep your code succinct in the future.

1
  • thanks for the help but I solved it with removing the extra for loops from the code. the for loops were making the system out of memory. for (int i = 97; i < 122; i++) { asc[i] = i; }
    – maysam
    May 20 '16 at 16:17

You must log in to answer this question.

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