0

my code is

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



int main(int argc, string argv[])
{
    if (argc == 1 || argv < 0 || argc > 2 )
    {
        printf("Error in command line");
    }
    else 
    {
        int k = atoi(argv[1]);
        printf("plaintext :");
        string plain = GetString();
        int size = strlen(plain);
        string cipher;
        cipher = malloc(size);
        for (int i = 0; i < size; i++)
        {
            cipher[i] = ((plain[i] + k ) % 26);
        }
        printf("ciphertext :");
        for (int j = 0; j < size; j++)
        {
            printf("%c", (char)cipher[j]);
        }
    }
    printf("\n");
}

i am notgetting the output can anyone help me

0
  1. Check the result of GetString for not being NULL, as that's what it returns instead of an empty string.
  2. Instead of argc == 1 || argc > 2, you could say argc != 2, which is much clearer. There are certain cases in which argc may be 0, though they are probably not relevant.
  3. Comparing argv to a number is not a good idea, as argv is a char**, and not a number. It's a memory address that one could interpret as a number, but don't, unless you know what you're doing (unlikely).
  4. Non-alphabetic characters may not be changed.
  5. Alphabetic characters need to be shifted to range 0..25 before adding k and using %26 (for example by subtracting 'A' or 'a', whichever is appropriate), and after applying Caesar, you'd need to shift it back (for example by adding 'A' or 'a', the same one you subtracted before)

You must log in to answer this question.

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