I think I'm close but something is a little off with my algorithm. I'm writing a Caesar encryption and when I type: HELLO, it should return IFMMP but it's returning this:
I'm taking a CS class online, just auditing the course. We haven't learned about pointers quite yet, so not sure if that's the problem?
Here's my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
// User enters parameters for the argument
// Get the argument for argv[1], read in the key in that positon of the argument
char *key = argv[1];
// convert keyt to an integer
int key2 = atoi(key);
int cipherValue;
char cipherLetter;
char str[30];
// Define variable for input
char text[30];
// Get user input
printf("\nplaintext: ");
scanf("%s", text);
// get length of the plaintext
long textLen = strlen(text);
// create lookup array, for each lowercase letter in alphabet (a - z), starting from index 0 to 25
char lettersLower[26];
int index_l = 0;
for (int i = 97; i < 123; i++) {
// implicitly convert int to char
char c = i;
// add char to lookup table index
lettersLower[index_l] = c;
// increment the index counter
index_l++;
}
// create lookup array, for each uppercase letter in alphabet (A - Z), starting from index 0 to 25
char lettersUpper[26];
int index_u = 0;
for (int i = 65; i < 91; i++) {
// implicitly convert int to char
char c = i;
// add char to lookup table index
lettersUpper[index_u] = c;
// increment the index counter
index_u++;
}
// for each letter in the plaintext word, get the index of the letter from the array and add 1, then take the modulo of 26 to get the ciphertext index
for (int i = 0; i < textLen; i++) {
// get each letter in the text
char plainLetter = text[i];
int asciiValue = plainLetter;
// lookup the value in lowercase letters array, if it's a lowercase letter then...
if (asciiValue > 96 && asciiValue < 123) {
for (int j = 0; j < 26; j++) {
// if the value is found then
if (plainLetter == lettersLower[j]) {
// add the value(j) plus 1 % 26, to get the ciphertext value
cipherValue = (j + key2) % 26;
// lookup new value in letter array
for (int k = 0; k < 26; k++) {
// if the ciphervalue equals the key's value then print out the new letter
if (cipherValue == k) {
// implicitly convert int to char value
cipherLetter = lettersLower[k];
//printf("this is concat letter: %c\n", cipherLetter);
// concatenate each letter
strcat(str, &cipherLetter);
}
}
}
}
}
//lookup the value in uppercase letters array, if it's an uppercase letter then...
else if (asciiValue > 64 && asciiValue < 91) {
// search the lookup table
for (int l = 0; l < 26; l++) {
// if the uppercase value is found
if (plainLetter == lettersUpper[l]) {
// add the value(l) plus 1 % 26, to get the ciphertext value
cipherValue = (l + key2) % 26;
// lookup new value in letter array
for (int m = 0; m < 26; m++){
// if the ciphervalue equals the key's value then print out the new letter
if (cipherValue == m) {
// implicitly convert int to char value
cipherLetter = lettersUpper[m];
// concatenate each letter
strcat(str, &cipherLetter);
}
}
}
}
}
}
// print out concatenated string
printf("ciphertext: %s\n", str);
}