0

Here is my code. Please help me where am I wrong?

Firstly I'm including next libraries:

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h> //for converting argv to int
#include <ctype.h> //for isalpha function


int main (int argc, string argv[])  
{
    if (argc != 2)
    {
        printf("missing the key\n");
        return 1;
    }

    char* key = argv[1];

    for (int k = 0; k < strlen(key); k++) //check every char in key
    {
        if (!isalpha(key[k]))
        {
            printf("key should be the alphabetical\n");
            return 1;
        }
    }

    printf("plaintext: ");
    char *s = get_string();

    int j;

    for (j = 0; j < strlen(s); j++) //qty of chars in string
            {
                j = j % strlen(key); //to loop the key word through string
            }

    if (s != NULL)
    {
        for (int i = 0; i < strlen(s); i++) 
            {
                if (isalpha(s[i])) //if chars are alphabet
                {
                    if (islower (s[i])) //if alpabet chars is lowerc
                    {
                         if (s[i] + key[j] > 122)
                         {
                             s[i] = s[i] - 26;
                         }
                    printf("%c", s[i] + key[j]);
                    }

                    else if (isupper (s[i])) //if alpabet chars is uppercase
                    {
                         if (s[i] + key[j] > 90)
                         {
                             s[i] = s[i] - 26;
                         }
                    printf("%c", s[i] + key[j]);
                    }
                }
                else
                {
                    printf("%c", s[i]);
                }
            }
        printf("\n");  
    }
    return 0;
}

3 Answers 3

1

Firstly your only checking if the first letter in the key is alphabetical. If i typed H3ll0 as the key your program won't return an error to the user when it should.

But other than that i don't see anything, but what prompts you that your code is wrong? does check50 not work, does it not compile, explain whats wrong and maybe I can help more.

2
  • thanks. changed. What's else? check50 told that it "killed by server" Commented Jan 11, 2017 at 14:36
  • Any other answers? Please help to understand my mistakes Commented Jan 18, 2017 at 15:35
0

my problem isn't solved please help me where I'm wrong. I have 2 ideas: 1) uncorrect looping key throgh plaintext or 2) uncorrect calculations of plaintext's chars with key's chars

0

A major problem is j. It is "calculated" here

   for (j = 0; j < strlen(s); j++) //qty of chars in string
            {
                j = j % strlen(key); //to loop the key word through string
            }

When program goes through s in the i loop, the value of j never changes. That calculation is doing the wrong thing in the wrong place.

j needs to be incremented (conditionally) in the i loop, and "wrap around" when it gets to the strlen(key). In other words it can never be => strlen(key).

A second major problem is the actual ciphertext calculation. You are adding the ascii value of the character in the key to the character in the plain text. You need to add the "alphabetic" value (eg a|a = 0, b|B = 1, etc).

After those things are fixed, you may have to work out other problems. You will still have problems passing check50. There is something "missing" that is required by the spec.

You must log in to answer this question.

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