#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h> //atoi ?
bool is_valid_key( string s);
// Validate the key as a string
// make sure each character is a digit
void encrypt(string plaintext, string chiphertext, int k);
int main (int argc, string argv[])
// Validate command line arguments
{
if (argc != 2 || !is_valid_key(argv[1])) // Valid key
{
printf("Usage: ./caesar key");
return 1;
}
int k = atoi(argv[1]); // #include <stdlib.h>....atoi
string s = get_string("plaintext: ");
int n = strlen(s);
char ciphertext[n + 1];
encrypt(s, ciphertext, k );
printf("ciphertext: %s/n", ciphertext);
return 0;
}
void encrypt(string plaintext, string ciphertext, int k)
{
int i = 0;
for (i = 0; i < strlen(plaintext); i++ )
{
char ch = plaintext[i];
if(isalpha(ch))
{
//Encrypt
// ci = (pi + k) % 26
//pi = current character
char temp = tolower(ch);
int pi = temp - 'a';
char ci = ((pi + k) % 26) + 'a';
ciphertext[i] = islower(ch ? ci : toupper(ci));
}
else
{
ciphertext[i] = ch;
}
}
ciphertext[i] = '\0';
}
bool is_valid_key( string s)
//write a for loop function to validate string
{
for (int i = 0; i < strlen(s); i++)
{
char ch = s[i];
if (!isdigit(ch))
{
return false;
}
}
return true;
}
"This what I come up with...."
~/pset2/ceaser/ $ make ceaser clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow ceaser.c -lcrypt -lcs50 -lm -o ceaser ~/pset2/ceaser/ $ ./ceaser 1 plaintext: a ciphertext: /n~/pset2/ceaser/ $