I am making a function out of the encryption process (since I wrote two identical ones minus the subtraction value drawn from ASCII).
One of the arguments is an array which I declared in the function as int x[]
. My code will not compile if I call the array keyword_num[]
, but it will if I just use keyword_num
. The error I get is "expected expression", when I attempt to compile it with keyword_num[]
. If I call keyword_num
it compiles but doesn't encrypt correctly, as the commented out block below it does.
My question is if I am not passing an array as an argument correctly, how would I do so?
Below is my code so far, with the array called as keyword_num[]
.
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define UPPERMAX 90
#define LOWERMAX 122
#define NUMLETTERS 26
void vencrypt(int r, int x[], int y, int z, string q);
int main(int argc, string argv[])
{
// declares counter for keyword array
int keycount = 0;
// checks if correct number of arguments provided
if (argc == 2)
{
// cycles through argument
for (int i = 0, n = strlen(argv[1]); i < n; i++)
{
// checks if each character is a letter
if (isalpha(argv[1][i]) == false)
{
// prompt reminding user only letters allowed in argument
printf("Please submit a command line arguement containing only letters.\n");
// returns error
return 1;
}
}
}
// ensures there is one command line argument
else
{
// prompt reminding user only one command line argument is allowed
printf("Please only submit one command line argument.\n");
// returns error
return 1;
}
// stores argument as keyword
string keyword = argv[1];
// creates an array for the corresponding numeric values to be stored in
int keyword_num[strlen(argv[1])];
// converts keyword into an array of corresponding numbers
for (int i = 0, n = strlen(argv[1]); i < n; i++)
{
if (isupper(keyword[i]))
keyword_num[i] = (int) keyword[i] - 65;
if (islower(keyword[i]))
keyword_num[i] = (int) keyword[i] - 97;
}
// gets user input and stores it in plaintext
string plaintext = GetString();
// runs through length of plaintext
for (int i = 0, n = strlen(plaintext); i < n; i++)
{
// resets spot in keyword array when maxed
if (keycount == strlen(keyword))
keycount = 0;
// checks if character is letter
if (isalpha(plaintext[i]))
{
// checks if letter is uppercase
if (isupper(plaintext[i]))
{
vencrypt(i, keyword_num[], keycount, UPPERMAX, plaintext);
// converts letter to ascii equivalent
// int upper_number = (int) plaintext[i];
// adds shift value
// int upper_encode = upper_number + keyword_num[keycount];
// loops from z to a (in ascii numbers)
// if (upper_encode > 90)
// upper_encode = upper_encode - 26;
// converts new number into letter
// char upper_letter = (char) upper_encode;
// prints new letter
// printf("%c", upper_letter);
// increments spot in keyword array
// keycount++;
}
else
{
vencrypt(i, keyword_num[], keycount, LOWERMAX, plaintext);
// converts letter to ascii equivalent
// int lower_number = (int) plaintext[i];
// adds shift value
// int lower_encode = lower_number + keyword_num[keycount];
// loops from z to a (in ascii numbers)
// if (lower_encode > 122)
// lower_encode = lower_encode - 26;
// converts new number into letter
// char lower_letter = (char) lower_encode;
// prints new letter
// printf("%c", lower_letter);
// increments spot in keyword array
// keycount++;
}
}
else
{
// prints character untouched
printf("%c", plaintext[i]);
}
}
// prints new line
printf("\n");
// returns a value of zero
return 0;
}
void vencrypt(int r, int x[], int y, int z, string q)
{
// converts letter to ascii equivalent
int number = (int) q[r];
// adds shift value
int encode = number + x[y];
// loops from z to a (in ascii numbers)
if (encode > z)
encode = encode - NUMLETTERS;
// converts new number into letter
char letter = (char) encode;
// prints new letter
printf("%c", letter);
// increments spot in keyword array
y++;
}