Я не знаю, смотрел ли я на это слишком долго, но мои персонажи остались прежними, т. Е. Если мой ключ «b» и напечатан «hello» в виде открытого текста, он все равно дает мне «Привет'.Кажется, я не могу понять, чего мне не хватает, чтобы заставить мой алфавит смещаться соответствующим образом, т.е. я проиндексировал свой алфавит 0-25
Заранее благодарен за любую помощь.
enter code here
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int shift(char c);
int main(int argc, string argv[])
{
// Ensure the program has only one command-line
if (argc != 2)
{
printf("Usage: ./vigenere key\n");
return 1;
}
else
{
// Ensure user only enter key in alphabet
string s = (argv[1]);
//Make sure command line argument is alpha
for (int i = 0; i < strlen(s); i++)
{
if (isalpha(s[i]) == false)
{
printf("Usage: ./vigenere key\n");
return 1;
}
}
// Converting string to integers
int key = atoi(argv[1]);
// Prompt user input in plaintext
string plaintext = get_string("plaintext: ");
printf("ciphertext: ");
for (int i = 0; i < strlen(plaintext); i++)
{
// Encrypt and preserve lower case characters
if islower(plaintext[i])
{
// Position of the character in the alphabet
int alphaPos = plaintext[i] + key - 97;
// Keep the number within the alphabet loop
int wrappedChar = alphaPos % 26;
printf("%c", wrappedChar + 97);
}
else if isupper(plaintext[i])
{
int alphaPos = plaintext[i] + key - 65;
int wrappedChar = alphaPos % 26;
printf("%c", wrappedChar + 65);
}
else
{
printf("%c", plaintext[i]);
}
}
printf("\n");
}
return 0;
}
int shift(char c)
{
//TODO
// If isUpper then minus 65
if (isupper(c))
{
return c - 65;
}
// If isLower then minus 97
else if (islower(c))
{
return c - 97;
}
return 0;
}