2
encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
#   Checks  
0    Running...  clang -o vigenere vigenere.c -lcs50 -lm. ✔
1    Expecting an exit code of  0. ✔
2    Running...  ./vigenere baz. ✔
3    Takes in standard input of  world, say hello!. ✔
4    Expecting the following on standard out —  xoqmd, rby gflkp!\n  ... but received the following on standard out instead —  xoqmd, szz gflkp!

My vigenere.c fails on only this test case. The rest pass properly. I'm no sure if posting my source code here would violate the honor code, hence posting just the error instead.

I'm supposed to get rby instead of szz for the plaintext - say, otherwise the rest of the ciphertext is also right as you can see.

Any ideas as to what I might be doing wrong?

UPDATE:

I'm trying to implement @superindesu's suggestions but the key that I am trying to use is generating funny.

I am using isalpha(char_variable) and ispunct(char_variable) but it's misreading a portion of the input and generating a wrong key. For debugging purposes, I've used - to indicate output from ispunct() and # to indicate output from an else block which catches anything other than alphabets or punctuations:

jharvard@appliance (~/Dropbox/pset2): ./vigenere-2 baz
world, say hello!
bazba-#zba#-#zba-# - key

Any ideas on how to fix this?

UPDATE 2: (Code)

string plaintext = GetString();

int keylen = strlen(key);
int ptlen = strlen(plaintext);

int j=0;
for(int i=keylen;i <= ptlen;i++){
    if(isalpha(plaintext[i])){
        key[i]=bigkey[j];
        j++;
    }
    else if(ispunct(plaintext[i])){
        key[i]='-';
    }
    else{
        key[i]='#';
    }
    key[i+1]='\0';
}

printf("%s - key\n",key);

// bigkey is a string that has the key repeated to the length of the plaintext.
// key is the key provided at the command-line by the user 
7
  • For such questions, @Nitinvenkatesh, just so we are able to help you better, please include your code so that we can atleast know what we're waking with. :D
    – Nib
    Jun 23 '14 at 10:19
  • Sorry about that @Nib , was not sure if that would be breaking the honor code of the class. Updated the question with the code so you can take a look :) Thanks :) Jun 23 '14 at 10:23
  • Nah, don't worry, it's just that It'll make it easier for people to know where you might be going wrong, else they'll have to shooting arrows in the dark :)
    – Nib
    Jun 23 '14 at 10:25
  • I see what you want to do, but it won't work that way... I think you're overcomplicating the problem... if you encode the whole text, then I will add 'a' when I don't want to do any changes to the text (meaning a rotate of 0)... '-' was just to make visible the parts you don't want to encode.
    – supeindesu
    Jun 23 '14 at 11:19
  • hmmm... even if I use a it's the same issue. I used - and # for clarity's sake here. Plus in the next segment I only take into consideration, places in the key variable that have corresponding alphabets in the plaintext. Jun 23 '14 at 14:37
2

2 points:

1- The key mustn't be applied until the character is alphabetical, so (- means no encoding)

You are doing this:
String: w o r l d ,   s a y   h e l l o !
Key:    b a z b a z b a z b a z b a z b a

While the right approach is:
String: w o r l d ,   s a y   h e l l o !
Key:    b a z b a - - z b a - z b a z b -

2- You have forgotten to include a return code at the end of the encoded line "\n"

4
  • Thanks for the quick reply. I'll try to hammer out the kinks and get back to you :) Jun 23 '14 at 8:51
  • Keep the good job.
    – supeindesu
    Jun 23 '14 at 8:51
  • Now ran into another problem, please take a look at the updated question. I am really hoping you can help me out. Thanks :) Jun 23 '14 at 10:13
  • Exactly! I was pondering answering until I saw this.
    – ChiCubed
    Jul 21 '14 at 4:22

You must log in to answer this question.

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