0

Hi I hope somebody can help! my program works fine but only when it gets to the end of the keyword it passes one character and doesn't print.

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

int main(int argc, string argv[])
{
    //check if the command line has two argument
    if(argc == 2){
        for(int k=0; k<strlen(argv[1]); k++){
        if(!isalpha(argv[1][k]))
        printf("Error!!!\n");//print out Error if there is more
    }}
       else {printf("Error!!!\n");
        return 1; 
       }

    printf("plaintext:");//print to the user
    string n = get_string();//get string from the user
    int len = strlen(n); //calculate the length of the string
    int l = strlen(argv[1]);
    string s = argv[1];
    printf("ciphertext:");
    int j=0;
    for(int i=0; i<len; i++){
        if(isalpha(n[i]))
        {
        if(isupper(n[i]))
        {
            if(isupper(s[j])){
                s[j]=((s[j]-'A')%26);
                printf("%c", ((((n[i]-'A')+s[j%l])%26)+'A'));
            }
            if(islower(s[j])){
                s[j]=((s[j]-'a')%26);
                printf("%c", ((((n[i]-'A')+s[j%l])%26)+'A'));
            }
            j++;
        }
        if( islower(n[i]))
        {
            if(isupper(s[j])){
            s[j]=((s[j]-'A')%26);
            printf("%c", ((((n[i]-'a')+s[j%l])%26)+'a'));}
            if(islower(s[j])){
                s[j]=((s[j]-'a')%26);
                printf("%c", ((((n[i]-'a')+s[j%l])%26)+'a'));
            }
            j++;
        }
        }
        if(isspace(n[i]))// ckech the index if space 
            printf(" ");
    }
    printf("\n");
    return 0;
}

and my output is like this:

~/workspace/pset2/ $ ./vigenere panda
plaintext:I like you
ciphertext:X lvne oh

As you can notice it jumps off the 'y'. I tried to insert if statement as

if(j==(l-1)) 
    j++;

so it can check to increase it to 5 in order to wrap around!

0

One problem is that you use j without % l. An alternative is to have % l applied on assigning, like

j = (j + 1) % l; // instead of j++

Another problem is that your code expects s to contain alphabetic characters only. After you've hit any of the s[j]=((s[j]-'A')%26); lines, that's no longer the case.

Either process the keyword once, but then don't do it in the loop over plain text, but in a separate loop before. Note that the resulting array cannot be used with string functions like strlen, as it might contain 0 as a valid value (you can use the length of the unprocessed keyword string, so that's not really a problem, but others have done that before).

Or, process the current key character only, storing the resulting shift in its own variable, or calculate it on the fly, and not store it at all, like

printf("%c", (n[i] - 'A' + s[j % l] - 'a') % 26 + 'A');

for uppercase letter encrypted using a lowercase keyword letter.

BTW, your "rejection" of a non-alphabetic keyword is somewhat soft. You print "Error!!!", but the code proceeds to use it.

You must log in to answer this question.

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