I am using character array and none of the test cases matches the result. Please help me find what is missing.
:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key
expected "ciphertext: b\...", not "ciphertext: b\..."
:( encrypts "barfoo" as "yxocll" using 23 as key
expected "ciphertext: yx...", not "ciphertext: yx..."
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
expected "ciphertext: ED...", not "ciphertext: ED..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
expected "ciphertext: Fe...", not "ciphertext: Fe..."
:( encrypts "barfoo" as "onesbb" using 65 as key
output not valid ASCII text
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
expected "ciphertext: ia...", not "ciphertext: ia..."
:) handles lack of key
:) handles non-numeric key
:) handles too many arguments
Here is my code.
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include<ctype.h>
#include <cs50.h>
#include <math.h>
int main(int argc, string argv[])
{
// check for only 2 arguments
if (argc == 2)
{
int key = atoi(argv[1]);
int notDig = 1;
// check for digits in the argument
for(int i = 0, n = strlen(argv[1]); i < n; i++)
{
if(!isdigit(argv[1][i]))
{
notDig = 0;
}
}
if (notDig)
{
string text = get_string("plaintext: ");
int ciLen = strlen(text);
char ciText [ciLen];
for (int i = 0; i < strlen(text); i++)
{
if (text[i] >= 'a' && text[i] <= 'z')
{
//for lowercase
if (text[i] + key > 122)
{
int thisChar = (text[i] + key)% 122+96;
ciText [i] = thisChar;
}
else
{
int thisChar = (text[i] + key)% 122;
ciText [i] = thisChar;
}
}
else if (text[i] >= 'A' && text[i] <= 'Z')
{
//for uppercase
if (text[i] + key > 90)
{
int thisChar = (text[i] + key)% 90+64;
ciText [i] = thisChar;
}
else
{
int thisChar = (text[i] + key)% 90;
ciText [i] = thisChar;
}
}
else
{
// for other characters
ciText [i] = text[i];
}
}
printf("ciphertext: %s",ciText);
}
else
{
return 1;
}
}
else
{
return 1;
}
return 0;
}