I am trying to make an encrypter, but as you can see, it's not working.
str[] have an undeclared array size, i know that theres an option to make it with a pointer array, but i really
want to make it a fixed size, also, so i can skip the "int strSize = sizeof(str);" in every function.
Hope that i makes sense and the code is not to messi.
// ENCRYPTING STRINGS
// 06-03-2012
// By Morten Lund
#include <stdio.h>
#include <stdlib.h>
void printstr(char str[]);
void crypt(char str[]);
void decrypt(char str[]);
#define ALPHA "abcdefghijklmnopqrstuvwxyz"
#define CRYPT_ALPHA "zyxwvutsrqponmlkjihgfedcba"
#define ALPHA_SIZE sizeof(ALPHA)
#define CRYPT_ALPHA_SIZE sizeof(CRYPT_ALPHA)
int main(int argc, char *argv[]) {
char str[] = "someword";
printf("Original string: ");
printstr(str);
crypt(str);
printf("\nCrypted string: ");
printstr(str);
decrypt(str);
printf("\nDecrypted string: ");
printstr(str);
printf("\n");
return EXIT_SUCCESS;
}
void printstr(char str[]) {
int i; int strSize = sizeof(str);
for (i = 0; i < strSize; i++) {
printf("%c", str[i]);
}
}
void crypt(char str[]) {
int i; int k; int strSize = sizeof(str);
for (i = 0; i < strSize; i++) {
for (k = 0; k < ALPHA_SIZE; k++) {
if (str[i] == ALPHA[k]) {
str[i] = CRYPT_ALPHA[k];
}
}
}
}
void decrypt(char str[]) {
int i; int k; int strSize = sizeof(str);
for (i = 0; i < strSize; i++) {
for (k = 0; k < CRYPT_ALPHA_SIZE; k++) {
if (str[i] == CRYPT_ALPHA[k]) {
str[i] = ALPHA[k];
}
}
}
}