I am trying to solve the vigenere.c problem, but there is one step that I need to figure out before my code works. I am stuck here and I don't know what else I could do. In my code, I have a variable called key_number that creates a number from 0 to 25 depending on what the letter(s) that is inputted into the command line is/are. This is accomplished through the help of a for loop.
When I try to use the key_number in another part of the code. An error message appears since it is no longer inside the for loop.
So, my question is: How do I take the value that is stored in the key_number and transfer it to the rest of the code so that my code can work?
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main(int argc, string argv[])
{
if (argc != 2) //Makes sure that only two strings are provided with the execute command.
{
printf("Usage: ./vigenere <key>\n");
return 1;
}
for (int i = 0, n = strlen(argv[1]); i < n; i++) //Makes sure that only alphabetica characters are inputted.
{
if (!isalpha(argv[1][i]))
{
printf("Error: Only alphabetic numbers please\n");
return 1;
}
}
string key = argv[1];
string plaintext = get_string("plaintext: "); //Asks user for input to his original text/plaintext.
printf("ciphertext: "); //Starts the printed output that will be followed up by the ciphertext.
for (int i = 0, n = strlen(key); i < n; i++) //Rotates through each letter of the input/key.
{
if (isupper(key)) //If the character is uppercase, that character is converted to its ASCII value then itis converted to a
//number from 0 to 25 depending on whilch letter of the alphabet it is.
{
for (int j = 65; j < 91; j++) //65 is ASCII value for 'A', 90 is ASCII value for 'Z'. So, it rotates through each
//possible value until the inputted character is found.
{
int key_number = (int) key[i];
if (key_number == j) //Checks to see if the integer value of a character matches the particular j value.
{
key_number = j - 65; //Converts ASCII value to a number from 0 to 25.
}
}
}
if (islower(key)) //If the character is lowercase, that character is converted to its ASCII value then itis converted to a
//number from 0 to 25 depending on whilch letter of the alphabet it is.
{
for (int j = 97; j < 123; j++) //97 is ASCII value for 'a', 122 is ASCII value for 'z'. So, it rotates through each
//possible value until the inputted character is found.
{
int key_number = (int) key[i];
if (key_number == j) //Checks to see if the integer value of a character matches the particular j value.
{
key_number = j - 97; //Converts ASCII value to a number from 0 to 25.
}
}
}
//PROBLEM STARTS HERE
if (isupper(plaintext[i])) //If the letter is uppercase, use the formula below to encrypt it and keep it uppercase.
{
char cipher_upper = (((plaintext[i] + key_number) - 65) % 26) + 65; //Creates variable for uppercase letters.
printf("%c", cipher_upper);
}
else if (islower(plaintext[i])) //If the letter is lowercase, use the formula below to encrypt it and keep it lowercase.
{
char cipher_lower = (((plaintext[i] + key_number) - 97) % 26) + 97; //Creates variable for lowercase letters.
printf("%c", cipher_lower);
}
else //If it is not alphabetic, keep it the same.
{
char cipher_neither = plaintext[i]; //Creates variables for non alphabetic characters.
printf("%c", cipher_neither);
}
}
printf("\n");
return 0;
}
Thanks for the help :)