0

I just wrote the program for cs50 pset2 vigenere problem. It ran perfectly when I tried to run it, with expected output. But on submitting, cs50.me check50 shows that my program does not show the output, when it definitely does.

It runs perfectly. Please help!

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int shift (char c);
int main(int argc, string argv[])
{
 if (argc != 2 || !isalpha(argv[1][0]))
 {
     printf("Usage: ./vignere keyword\n");
     return 1;
 }
 else
 {
     char key[100];
     for(int j=0; key[j] != '\0'; j++)
     {
         key[j] = shift(argv[1][j]);
     }
     string text = get_string("Plaintext: ");
     printf("ciphertext: ");
     int j = 0;
     for (int i = 0; text[i] != '\0'; i++)
     {            
         if (j == strlen(argv[1]))
         {
             j = 0;
         }
         if (islower(text[i]))
         {
             printf("%c",(((text[i] + key[j]) - 97) % 26) + 97);
         }
         else if (isupper(text[i]))
         {
             printf("%c",(((text[i] + key[j]) - 65) % 26) + 65);
         }
         j++;           
     }
     printf("\n");
     return 0;      
 }
}
int shift(char c)
{
 if (islower(c))
 {
     c -= 97;
 }
 else if (isupper(c))
 {
     c -= 65;
 }
 return c;
}

This is the link https://cs50.me/submit50/results/deku82/c9a713dbd8b4c5ed00e196906bbd8f3c23437440

1
  • That's a private repo, we cannot see it. Maybe use gist.github.com and post link here. Also, could you maybe provide a link to the check50 report? Edit those into your question, not as a comment. – Blauelf May 9 '19 at 9:56
1

You don't correctly reject keywords which are not completely alphabetic. For that, you'd need a loop over the keyword.

key[j] != '\0' should probably be argv[1][j] != '\0'. That's why input gets printed without any encryption applied.

You don't copy over non-letters. Those should appear unchanged, and not affect the position within the keyword (so in your case not increment j).

An if that returns at the end does not require an else (if you had taken the path, you would never have arrived at the code after the return), could save you some {} and indentation level.

You must log in to answer this question.

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