0

Showing following error

encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
   \ expected output, but not "ciphertext:xoqmd, szz gflkp!\n"

what should I make change in my command

my code is

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

// functions declarations
// check whether input is capital or small
int check(char x, int y);
// checks the argument line
int check1(char arg[]);
// changes the argument into digits as 'a' or 'A' as 0
int change(char argument);
// encrypt the capital letters
int capital(char x, int y);
// encrypt the small letters
int small(char x, int y);



int main(int argc, string argv[])
{
    // checking command line
    int check2 = check1( argv[1] );
    if (argc != 2 || check2)
    {
        printf("Error in command line\n");
        return 1;
    }
    else 
    {
        // chaninging argv into digit 
        int k[strlen(argv[1])];
        for (int i = 0; i < strlen(argv[1]); i++)
        {
            k[i] = change(argv[1][i]);
        }
        // asks user for plaintwext
        printf("plaintext:");
        string plain = GetString();
        // checks size of plaintext
        int size = strlen(plain);
        // for repeating the code word
        int repeat[size];
        for (int i = 0; i < size; i++)
        {
            int mod = i % strlen(argv[1]);
            repeat[i] = k[mod];
        }
        int cipher[size];
        // cipher the plaintext
        for (int i = 0; i < size; i++)
        {
            cipher[i] = check (plain[i], repeat[i]);
        }
        // prints the ciphered text
        printf("ciphertext:");
        for (int j = 0; j < size; j++)
        {
            printf("%c", (char) cipher[j]);
        }
    }
    printf("\n"); 
}

int check1(char arg[])
{
    int j = 0;
    if (arg != NULL)
    {
        int k = strlen(arg);
        for (int i = 0; i < k; i++)
        {
            if (isdigit(arg[i]))
            {
                j = 1;
            }
        }
    }
    return j;
}

// check whether input is capital or small
int check(char x, int y)
{
    int valid;
    if ( ( x >= 'A' ) && ( x <= 'Z' ) )
    {
        valid = capital ( x, y);
    }
    else
    {
        if (( x >= 'a' ) && ( x <= 'z' ))
        {
            valid = small ( x, y );
        }
        else
        {
            valid = x;
        }
    }
    return valid;
}
// changes the argument into digits as 'a' or 'A' as 0
int change(char argument)
{
    int code;
    if ( ( argument >= 'A' ) && ( argument <= 'Z' ) )
    {
        code = argument - 65;
    }
    else
    {
        code = argument - 97;
    }
    return code;
}
// encrypt the capital letters
int capital(char x, int y)
{
    int valid = x + y;
    while ( valid > 90)
    {
        valid = valid - 26;
    }
    return valid;
}
// encrypt the small letters
int small(char x, int y)
{
    int valid = x + y;
    while ( valid > 122)
    {
        valid = valid - 26;
    }
    return valid;
}
1
  • Did the pset get updated this year to require the phrase "ciphertext:" to be printed? If not, then this will cause every test to fail all by itself.
    – Cliff B
    Mar 3, 2017 at 11:19

1 Answer 1

0

First line in main is the explanation for your previous question

Do not use argv[1] before checking argc. You could avoid this by doing

if (argc != 2 || check1(argv[1]))

|| won't evaluate check1(argv[1]) if its left side already is true.

Your check1 rejects only digits, I'd use if (!isalpha(arg[i])) instead of if (isdigit(arg[i])) to reject any non-alphabetic character, including punctuation.

You should check the result of GetString or get_string for being NULL first, as that's what it returns instead of an empty string. Otherwise, you'll have a segfault whenever the user chooses to input nothing for the plaintext, and just presses Return.

Now to your question: It's important to note that the character of the keyword used in encryption advances only on encrypting a letter, so you'll need another index for the keyword, incremented independently. When you encrypt, or rather pass-through, a non-alphabetic character, that index would stay the same.

2
  • I have the same problem but I don't really understand this answer. Apr 29, 2017 at 6:42
  • @AngadSingh The problem leading to the wrong ciphertext is that you need to advance in the key only when encrypting a letter. So passing a non-letter (which is not encrypted) from plain-text to cipher-text does not "consume" a letter from the key.
    – Blauelf
    May 2, 2017 at 9:20

You must log in to answer this question.

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