1

check50 message: https://cs50.me/checks/c053757544cd74cafb77554759754b8ee4ee37a8

The code doesn't encrypts "a" as "a" using "a" as keyword. It adds a wierd character in the end and I don't know why. it also doesn't encrypts "world!$?" as "xoqmd!$?" using "baz" as keyword. It adds a capital "B" in the end. Can someone help me figure out where the problem is? Thanks.

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

int main(int argc, string argv[])
{
//check if key is available
if (argc != 2)
{
    printf("Please enter a key (and only the keyword) in the command line 
    argument.\n");
    return 1;
}
//check if keyword is alphabetic
for (int k = 0, t = strlen(argv[1]); k < t; k++)
{
    if (!isalpha(argv[1][k]))
    {
        printf("Please enter alphabetical characters only in the keyword.\n");
        return 1;
    }
}

//promt user for Plaintext
printf("plaintext: ");
string plain = get_string();

//declaring of variables
char cipher[strlen(plain)];
string key = argv[1];
int j = 0, temp1, temp2;

//looping with the number of characters in plaintext
for (int i = 0, n = strlen(plain); i < n; i++)
{
    //checking if char in plain is CAP or small
    if (isupper(plain[i]))
    {
        //checking if the char in keyword is CAP or small
        if (isupper(key[j % strlen(key)]))
        {
            //looping of j and converting ASCII to alphabetical order
            temp1 = ((int) key[j % strlen(key)] - 65);
        }
        else if (islower(key[j % strlen(key)]))
        {
            //looping of j and converting ASCII to alphabetical order
            temp1 = ((int) key[j % strlen(key)] - 97); 
        }
        //ciphering plain char with keyword
        cipher[i] = ((( (int) plain[i] - 65) + temp1) % 26) + 65; 
        j++;
    }
    else if (islower(plain[i]))
    {
        if (isupper(key[j % strlen(key)]))
        {
            temp2 = ((int) key[j % strlen(key)] - 65);
        }
        else if (islower(key[j % strlen(key)]))
        {
            temp2 = ((int) key[j % strlen(key)] - 97);
        }
        cipher[i] = ((( (int) plain[i] - 97) + temp2) % 26) + 97;
        j++;
    }
    else
        //if plain character is not alphabetical, char doesn't change
        cipher[i] = plain[i];
}
printf("ciphertext: %s\n", cipher);  //printing of ciphered text

return 0;
}

When i use check50 here is what I get:

:) vigenere.c exists.
:) vigenere.c compiles.
:( encrypts "a" as "a" using "a" as keyword
output not valid ASCII text
:) encrypts "barfoo" as "caqgon" using "baz" as keyword
:) encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
:) encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
:( encrypts "world!$?" as "xoqmd!$?" using "baz" as keyword
output not valid ASCII text
:) handles lack of argv[1]
:) handles argc > 2
:) rejects "Hax0r2" as keyword
3

You are printing garbage values in the final printf statement:

printf("ciphertext: %s\n", cipher);  //printing of ciphered text

You are asking it to print cipher as a string. You declared cipher as a character array like this:

char cipher[strlen(plain)];

That's fine, but it's not yet a string. printf and other functions like strlen need a character array to end in a \0 character so it knows where the string ends. printf is reading characters from the place in memory that cipher points to, but it won't stop until it finds a null character. If you re-run your program, you will find that it prints different garbage values every time - whatever bytes happen to be in memory.

The cs50 library function getString() takes care of putting the null character in the array for you, but if you make your own character array, you have to add the null character explicitly:

char cipher[strlen(plain)+1];
cipher[strlen(plain)] = '\0';

Now you are reserving an extra spot for the null character, and placing it at the end.

When printf prints cipher, now it will stop at the proper position.

| improve this answer | |
  • So, is the only difference between a string and a character array the null character? – Murad M. Tag Sep 2 '17 at 13:05
  • C doesn't really have a string. It only has char[]. When the professor was using string early in the class it was just an alias for char[] so he didn't have to explain pointers or arrays yet. If you want to use the functions in C that take a string for input - like printf or strlen- you always have to make sure there is a \0 to tell them where to stop. – robert_x44 Sep 2 '17 at 16:11

You must log in to answer this question.

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