im kinda stuck with pset2, substitution. Any help is appreciated. When trying to convert the plaintext into the ciphertext, the formula just doesnt do anything. I suppose there is something wrong with it, but what is?
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
bool letters(string x, int y);
string cipher(string x, string g, int y);
int main(int argc, string argv[])
{
int y = strlen(argv[1]);
bool check = letters(argv[1], y);
if (argc != 2 || y != 26 || check != true)
{
printf("Usage ./substitution key\n");
return 1;
}
string plaintext = get_string("plaintext: ");
string ciphertext = cipher(plaintext, argv[1], y);
printf("ciphertext: %s\n", ciphertext);
}
bool letters(string x, int y)
{
bool j;
for (int i = 0; i < y; i++)
{
for (int k = 1 + i; k < y; k++)
{
if (toupper(x[i]) == toupper(x[k]))
{
j = false;
return j;
}
}
if (!isalpha(x[i]))
{
j = false;
}
else
{
j = true;
}
}
return j;
}
// itinerates over the plain text
string cipher(string x, string g, int y)
{
for (int i = 0; i < y; i++)
{
// itinerates over each letter of the plaintext and checks if it matches the key letters. if it
//does, replaces the ith letter for the gth one
for (int k = 0, len = strlen(g); k < len; k++)
{
int h = 0;
h = (g[k] % 90) + 65;
h += k;
if (isalpha(x[i]))
{
if (isupper(g[k]) && toupper(x[i]) == h)
{
x[i] = g[k];
}
}
}
}
return x;
}
The cipher function isnt finished yet, but shouldnt it convert the plaintext if it is lowercase and the key is uppercase?
The section with comments on the code is the one that isnt working.