0

int main (int argc,string argv[]) { string key=argv[1];//get the user key

if (argc!=2) //check the user input 
{
    printf("Command line argument is not valid\n");
    return 1;
}

if (!isalpha(argv[1]))

{ printf("Invalid characters\n"); return 1; }

printf ("plaintext: ");
string p=get_string(); //get the plaintext

printf ("ciphertext: ");
for(int i=0, n=strlen(key);i<n;i++)
{


    if (isalpha(key[i]))

    {
        if (isupper(key[i]))
        {
            key[i]=key[i]-65;

        }

        if (islower(key[i]))
        {

            key[i]=key[i]-97;
        }

    }
     printf ("%c",i) ;
    for(int j=0,k=strlen(p);j<k;j++)
{
    if (isupper(p[j]))
    {
        p[j]=((p[j]-65)+key[i])%26 + 65;

    }
    if (islower(p[j]))
    {
        p[j]=((p[j]-97)+key[i])%26 + 97;
    }

  printf("%c",j) ;
}
}

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

printf("\n");

}

1
  • In the editor, select all the code, and click the {} button. It adds/removes four spaces in front of every line, and four spaces in front of a line indicate a code block in Stackexchange-flavoured markdown.
    – Blauelf
    Apr 21, 2017 at 13:10

1 Answer 1

2

Do not read argv[1] before you have checked argc. If argc<2, argv[1] does not even exist, it's outside of the argv array.

if (!isalpha(argv[1]))

won't work. isalpha takes a char, while key/argv[1] is a char*. You'll have to build a loop in which you check the characters in argv[1] individually.

Your loop structure seems a bit off. It's ok if you change your key to an array of shift values first, but your loop over the text should not be inside that one.

When encrypting the string, you'd start with two indices, both initially 0.

One indicates the position inside the plain text, one the position inside the key.

Inside your text loop, the position inside the plain text is incremented for each character, for walking through the text.

At the same time, the index for the key is incremented only after encrypting a letter, and wrapped around (for example by using %keylen or setting the value to 0 once it has reached keylen).

That's a single loop for the text (plus maybe one extra if you want to pre-process your key to hold shift values instead of characters).

BTW, I totally not get your printf ("%c",i). You print an index interpreted as a character?

And if you have a list of ifs, and you want to have at most one of them match, you'd better use like if-else if instead of if-if (maybe followed by a final else)

2
  • I m not sure how to change the key to the array of shift values. Will I construct the array ?
    – ScriptK
    Apr 24, 2017 at 16:13
  • Either create a new array of shift values, or re-use the array of char, since char is just a (depending on implementation signed or unsigned by default, though you can override that) integer, usually 8-bit, so with values from 0 to 255 or -128 to 128. You'd have to remember the key's length in that case, as 0 would now be a valid value, not the terminator. Or do that transformation inside your main loop whenever you need to encrypt a letter (then transform only one key character, and don't change the key), but for this problem, do not at any point nest loops.
    – Blauelf
    May 2, 2017 at 9:30

You must log in to answer this question.

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