Hello guys, so I have to write a program that will use Vigenere cipher, i.e. encrypt every letter using a keyword. (A/a in keyword represent 0 etc.)
My code so far looks like this
Code:
#include <stdio.h>#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, string argv[])
{
string c = argv[1];
int l = strlen(c);
//convert keyword into separate "keys", i.e. A/a = 0 etc.
for(int i = 0; i < l; i++)
{
if(c[i] < 123 && c[i] > 96)
c[i] = c[i] - 97;
else if(c[i] < 91 && c[i] > 64)
{
c[i] = c[i] - 65;
}
printf("%d \n", c[i]);
}
string plain = GetString();
for(int i = 0; i < strlen(plain); i++)
{
//if we get to i-th position in a plaintext and the i-th position in key does not exist, set this i-th position to c[i - length]
if(i > l)
c[i] = c[i - l];
else
//if lowercase, then (plain[i] - 97) + ((c[i])%26) + 97
//if uppercase, then (plain[i] - 65) + ((c[i])%26) + 65
//if not a letter, print the original value
//print each crypted letter
}
}
The whole thing should work on texts without the punctuation and spaces, however, it won't work on texts with them; if the first "letter" to encrypt would be a space, the program would print the space as needed, however for second letter, the program would use the 2nd key in keyword, which we do not want.
If banging my head against the wall whole day, I really can not figure this out, so I came here for help. Please, keep in mind that I just began to code, so apparently the code I've written is not going to be the most effective one.
Any help is much appreciated.
//edit: I figured it literally second after I posted, all I have to do is introduce a new integer that will keep track of position in keyword, if the letter is going to be encrypted, increment this value by one, if it is not, keep it as it was. Q_Q