#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
bool only_digits(string s);
char rotate(char c, int n);
int main(int argc, string argv[])
{
if (argc < 2 || only_digits(argv[1]) == false || argc > 2) // check for th correct input
{
printf("usage: ./caesasr key \n");
}
else
{
int key = atoi(argv[1]);
string plaintext = get_string("plaintext: ");
int x = strlen(plaintext);
char cyphertext[x + 1];
for (int i = 0; i < x; i++)
{
cyphertext[i] = rotate(plaintext[i], key);
}
printf("cyphertext: %s\n", cyphertext);
}
}
//make a function bool only_digits to check for input is a single digit between 0-9
bool only_digits(string s)
{
if (s[0] > 47 && s[0] < 58 && strlen(s) == 1)
{
return true;
}
else
{
return false;
}
}
//make a function char rotate(char c, int n) that rotate c by n on the alpahbet
// cout = (cin -65 + n)%26 +65 (uppercase)
// cout = (cin -97 + n)%26 +97 (lowercase)
// cout = cin (other)
char rotate(char c, int n)
{
if (c > 64 && c < 91)
{
c = ((c - 65 + n) % 26 + 65);
}
if (c > 96 && c < 123)
{
c = ((c - 97 + n) % 26 + 97);
}
else
{
return c;
}
return c;
}
CLI outputs, question marks and chars added randomly to the cyphertext
I can't figure out where the question marks and randoms chars come from; it seems it only works with a 5-letter input; debug command finds everything works as intended until the last output letter is printed then suddenly random chars are added.
EDIT: changed line 21 to
char cyphertext[x];
did not fix the problem
The output string is not null terminated: you added space for the null byte at the end, but you did not set it explicitly. The array
cyphertextis uninitialized, so the character atcyphertext[i]may be anything andprintfoutputs it, then keeps reading and printing characters present in memory after the array, which is undefined behavior.You should set the null terminator after the
forloop withAlso note these problems:
'A'instead of explicit ASCII codes like65.only_digitsshould test all characters and accept strings longer than 1 byte for key values up to 25.Here is a modified version: