1
int main(int argc, string argv[])
{
    string k;
    k = argv[1];
    if(argc == 2)
    {
        for(int i=0,j=strlen(k); i<j; i++)
        {
            if( isdigit(k[i]))
            {
                printf("Invalid Arguments!!!\n");
                return 1;
            }
        }
    }
    else
    {
        printf("Invalid Arguments!!!\n");
        return 1;
    }

    printf("Enter Plain Text: ");
    string p = GetString();
    printf("%s\n", p);
    int y = strlen(k);
    for(int i=0,j=strlen(p); i<j; i++)
    {
        if( i < y)
        {
            int cipherku;
            int cipherpu;
            int cipherkl;
            int cipherpl;
            cipherku = k[i] - 'A';
            cipherkl = k[i] - 'a';
            cipherpu = p[i] - 'A';
            cipherpl = p[i] - 'a';
            if(isalpha(p[i]))
            {
                if(isupper(p[i]) && isupper(k[i]))
                {
                    if(cipherpu+cipherku >= 26)
                    {
                        int r;
                        int c;
                        r = (cipherpu+cipherku)%26;
                        c = r + 65;
                        printf("%c", toupper(c));
                    }
                    else
                    {
                        printf("%c",toupper(p[i]+cipherku));
                    }
                }
                else if(isupper(p[i]) && islower(k[i]))
                {
                    if(cipherpu+cipherkl >= 26)
                    {
                        int r;
                        int c;
                        r = (cipherpu+cipherkl)%26;
                        c = r + 65;
                        printf("%c", toupper(c));
                    }
                    else
                    {
                        printf("%c",toupper(p[i]+cipherkl));
                    }
                }
                else if(islower(p[i]) && isupper(k[i]))
                {
                    if(cipherpl+cipherku >= 26)
                    {
                        int r;
                        int c;
                        r = (cipherpl+cipherku)%26;
                        c = r + 97;
                        printf("%c", tolower(c));
                    }
                    else
                    {
                        printf("%c",tolower(p[i]+cipherku));
                    }
                }
                else if(islower(p[i]) && islower(k[i]))
                {
                    if(cipherpl+cipherkl >= 26)
                    {
                        int r;
                        int c;
                        r = (cipherpl+cipherkl)%26;
                        c = r + 97;
                        printf("%c", tolower(c));
                    }
                    else
                    {
                        printf("%c",tolower(p[i]+cipherkl));
                    }
                }
            }

            else
            {
                printf("%c",p[i]);

            }
        }
        else
        {

            int x=0;
            x = i%y;


            int cipherku;
            int cipherpu;
            int cipherkl;
            int cipherpl;
            cipherku = k[x] - 'A';
            cipherkl = k[x] - 'a';
            cipherpu = p[i] - 'A';
            cipherpl = p[i] - 'a';
            if(isalpha(p[i]))
            {

                if(isupper(p[i]) && isupper(k[x]))
                {
                    if(cipherpu+cipherku >= 26)
                    {
                        int r;
                        int c;
                        r = (cipherpu+cipherku)%26;
                        c = r + 65;
                        printf("%c", toupper(c));

                    }
                    else
                    {
                        printf("%c",toupper(p[i]+cipherku));

                    }
                }
                else if(isupper(p[i]) && islower(k[x]))
                {
                    if(cipherpu+cipherkl >= 26)
                    {
                        int r;
                        int c;
                        r = (cipherpu+cipherkl)%26;
                        c = r + 65;

                    }
                    else
                    {
                        printf("%c",toupper(p[i]+cipherkl));

                    }
                }
                else if(islower(p[i]) && isupper(k[x]))
                {
                    if(cipherpl+cipherku >= 26)
                    {
                        int r;
                        int c;
                        r = (cipherpl+cipherku)%26;
                        c = r + 97;
                        printf("%c", tolower(c));

                    }
                    else
                    {
                        printf("%c",tolower(p[i]+cipherku));

                    }
                }
                else if(islower(p[i]) && islower(k[x]))
                {
                    if(cipherpl+cipherkl >= 26)
                    {
                        int r;
                        int c;
                        r = (cipherpl+cipherkl)%26;
                        c = r + 97;
                        printf("%c", tolower(c));

                    }
                    else
                    {
                        printf("%c",tolower(p[i]+cipherkl));

                    }
                }
            }
            else
            {
                printf("%c",p[i]);

            }

        }
    }
    printf("\n");

}
1

I am going to refer to line numbers based on the code you provided. For example:

int main(int argc, string argv[])    // Line #   1

printf("%c",toupper(p[i]+cipherkl)); // Line #  66

r = (cipherpl+cipherkl)%26;          // Line # 181

i is incrementing for EVERY SINGLE character in p. This includes alphabetical characters and non-alphabetical characters.

You want to increment your key progress only if the character is alphabetical.

I suggest you add a counter only for the key progress. This key is going to increment every time you encrypt an alphabetical character.

For example:

Somewhere before the loop declare the counter. Around lines 24-26:

int kc = 0; // Declaring keyword counter
int y = strlen(k);
for(int i=0,j=strlen(p); i<j; i++)

Then, assign your cipherku and cipherkl based on this counter. Lines 33 and 34:

cipherku = k[kc] - 'A';
cipherkl = k[kc] - 'a';

Also changing these assignments when you take care of wrapping around. Lines 110-119:

int x=0;
x = kc%y; // Use keyword counter instead of i.


int cipherku;
int cipherpu;
int cipherkl;
int cipherpl;
cipherku = k[x] - 'A'; // No changes here.
cipherkl = k[x] - 'a';

This variable is going to increment every time you have an alphabetical character. This is everywhere except in lines 101-105 and 193-197. Where you took care of non-alphabetical characters with this code:

else
{
    printf("%c",p[i]);

}

So how do we do that? Well, we increment in the ifs of these two elses. That will correspond to lines 99 and 192:

                    else
                    {
                        printf("%c",tolower(p[i]+cipherkl));
                    }
                }
                kc++; // Increment your keyword counter on lines 99 and 192.
            }

            else
            {
                printf("%c",p[i]);

            }
        }

However, this won't make check50 give you all happy faces. That is because check50 is too picky. So comment out or delete lines 22 and 24 in order to have all happy faces:

//printf("Enter Plain Text: ");
string p = GetString();
//printf("%s\n", p);
int y = strlen(k);
1
  • program worked fine and now i understand where i was going wrong .. Thank you :) Nov 10 '16 at 6:51
2

Please use argv[1] only after you've checked that argc is >=1, otherwise you might access an element that's after the array's end.

Instead of isdigit, I'd use !isalpha.

You could do that for example as

    if(argc != 2)
    {
        printf("Invalid Arguments!!!\n");
        return 1;
    }
    k = argv[1];
    for (int i = 0, j = strlen(k); i < j; i++)
    {
        if (!isalpha(k[i]))
        {
            printf("Invalid Arguments!!!\n");
            return 1;
        }
    }

Now to the really breaking part: You'll need some extra counter for the key, you cannot use i for both (so no k[i]). You'd increment that counter only if isalpha(p[i]), and wrap it around on reaching the key length (a bit like your x is probably meant to work), or generally use it as counter%keylength (with % modulo operator). A non-alphabetic character should not trigger advancing in the key.

You must log in to answer this question.

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