Skip to main content
edited tags
Link
kzidane
  • 17.7k
  • 3
  • 28
  • 100
Source Link

Cs50's vigerene encrypting problem

My code complies well but encrypts the text in the wrong way.Could someone help me find the problem?

any help will be appreciated

here is my code:

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

int main (int argc, string argv[])
{
if (argc != 2)
{
     printf("Please enter a valid key\n");
    return 1;
}

string key = argv[1];

for (int i=0 ,n=strlen(key);i<n; i++)
{
    if(!isalpha(key[i]))
    {
        return 1;
    }
}

printf("input:");

string text = GetString();
int k = 0;

for(int i = 0,n = strlen(text),j = k % strlen(key); i < n ; i++,j++)
{
   
    
    if(isupper(text[i]))
    {
        char c = ((text[i]-65 + key[j])%26) + 65;
        printf("%c",c);
    }
    
    else if (islower(text[i]))
    {
        char c = ((text[i]-97 + key[j])%26) + 97;
        printf("%c",c);
    }
    
    else
    {
        printf("%c",text[i]);
    }
    
}
 printf("\n");
 return 0;
}