0

Colleagues,

In vigenere, I can obtain and error check the required user inputs and I have a conceptual understanding of the steps required to encipher the plaintext with the keyword, looping where necessary.

However, I am struggling to achieve the correct enciphering and at this point have gone somewhat code-blind..

If anyone can take a look at the code and give me a hint as to where I am going wrong it would be much appreciated.

Relevant code:

for (int i = 0, k = 0; i < plaintext_length; i++)
   {
    if ( isalpha(plaintext[i]) && isupper(plaintext[i]))
    {
        encrypted_char = ((plaintext[i] - 65) + (keyword[k %   keyword_length]) % 26 + 65);
        printf("%c", encrypted_char);
        k++;
    }
    else if ( isalpha(plaintext[i]) && islower(plaintext[i]))
    {
        encrypted_char = ((plaintext[i] - 97) + (keyword[k % keyword_length]) % 26 + 97);
        printf("%c", encrypted_char);
        k++;
    }
    else if (isspace(plaintext[i]))
    {
        printf("%c", encrypted_char);
    }
    else
    {
        printf("%c", plaintext[i]);
    }
 }

Many thanks in advance!

2
  • Can you provide output vs expected output – Mustaghees Aug 12 '15 at 4:50
  • what is keyword_length? isn't keyword already an integer? Also why are you considering a special case when it is a space?, space should be the same that all other non alphabetical characters. – wallek876 Aug 12 '15 at 6:08
0

This problem was one of ASCII math(s) and looping. It is now solved. Other problems remain but I am confident they != insurmountable.

I absolutely love CS50, just saying.

6
  • Hello @Andrew Smith, even i am facing a very similar problem right now , can u suggest how u came out of it ? here's my enciphering code , string p = GetString(); for(int i=0,j=0,x=strlen(p);i<x;i++) { int j=i%strlen(k); if(isalpha(p[i])) { if(isupper(p[i])) { p[i]=(((p[i]-'A')+(k[j]-'A'))%26)+'A'; } if(islower(p[i])) { p[i]=(((p[i]-'a')+(k[j]-'a'))%26)+'a'; } } } printf("%s",p); – Sai Charan Nivarthi Aug 29 '15 at 14:25
  • Hi. I would be happy to try and help but your code is a little hard to read for a newcomer such as myself, could you use the code snippet tool to display it correctly or maybe put it in pastebin? – Andrew Smith Aug 29 '15 at 23:22
  • pastebin.com/QCkHRBrE Thanks in advance ! – Sai Charan Nivarthi Aug 30 '15 at 16:34
  • So what problem specifically are you facing? Have you tried using gdb to debug your code? – Andrew Smith Aug 30 '15 at 17:05
  • my problem is , enciphering occurs only for the first time traversal of keyword , here is my modified code , hope u help : pastebin.com/gFsvfWar – Sai Charan Nivarthi Aug 31 '15 at 14:26
0

I think your way of enciphering is correct and I want to know why you didn't convert the keyword-ASCII into Alphabet-index ( subtracting 65 ). ?

-4
encrypted_char = ((plaintext[i] - 65) + (keyword[k %   keyword_length]) % 26 + 65);

why do you use the % operator here? you should not be dividing but instead just be using k to do the indexing into the keyword, assuming that you first repeated the keyword such that is equal to or longer than the string that requires enciphering (otherwise you need to check the number of k such that it is never longer than the length of the keyword).

else if (isspace(plaintext[i]))
{
    printf("%c", encrypted_char);
}*/ 

not required and causes problems because encrypted_char not defined here.

1
  • -1 Don't repost the code with comments. If needed include the snippet of code this the problem and then write what is wrong with this snippet. Your answer is really hard to be understood. Also it's wrong. He's correctly using the length of the key that enciphers the plaintext. – ChrisG Aug 12 '15 at 8:39

You must log in to answer this question.

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