#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
bool isNumber(char number[]);
int main(int argc, string argv[])
{
// command line argument validation
if (argc <= 0 || argc > 2 || isNumber(argv[1]))
{
printf("error\n");
return 1; //error output
}
else
{
int i;
int j = 0;
char ascii;
string k = argv[1];
//ensure if the keyword is alphabetical, if not bye bye!
for (i = 0; i < strlen(k); i++)
{
if (isalpha(k[i]))
{
;
}
else
{
return 1;
}
}
printf("plaintext: ");
string plaintext = get_string();
printf("ciphertext: ");
// iterating over the plaintext
for (i = 0; i < strlen(plaintext); i++)
{
if (isalpha(plaintext[i]))
{
if (isupper(plaintext[i]))
{
ascii = (plaintext[i] - 65) + (toupper(k[j]) - 65) % 26;
printf("%c", ascii + 65);
}
else if (islower(plaintext[i]))
{
ascii = (plaintext[i] - 97) + (tolower(k[j]) - 97) % 26;
printf("%c", ascii + 97);
}
j++;
}
else{
printf("%c", ' ');
if (j >= strlen(k) - 1)
{
j = 0;
}
}
}
}
printf("\n");
return 0;
}
bool isNumber(char number[])
{
int i = 0;
//checking for negative numbers
if (number[0] == '-')
i = 1;
for (; number[i] != 0; i++)
{
//if (number[i] > '9' || number[i] < '0')
if (!isdigit(number[i]))
return false;
}
return true;
}
Please, I need help.
If I run the Walkthrough example I get a incomplete output:
$./vinegere panda plaintext: I like you ciphertext: X lvne o <- it's imcomplete
I'm reseting the j variable every time that it reaches the k(keyword) string len to start on index 0 again. The formula to swift is working but the iteration is messed up.
I need some insight, thanks.