Reputation: 65
I wrote the following code, in short it's supposed to "encrypt" the user inputted message according to the key array in the main function, say if the user inputs the word "holidays" then it should print it out as "qoervmhj", and if the input is "SILVER" then the output should be "LOSCTK" and so forth. Now when I try to run the code, it just prints the input without modifying it for some reason, I can't pinpoint the problem, I suspect it's got something to do with the encrypt_words function, my thought process was that since "words" is an array of pointers, I could just access the content of "words[i]", encrypt it and then copy it back to "words[i]", but now I am not sure, would appreciate any help.
/*-------------------------------------------------------------------------
Include files:
--------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KEY_SIZE 52
#define WORDS_ARRAY_SIZE 100
#define MAX_STR_LEN 50
#define ARR_PLACE 71
char encrypt_char(unsigned char key[KEY_SIZE], char ch);
int read_words(char* words[], int size, int max_str_len);
void encrypt_words(char* words[], int num_words, unsigned char key[KEY_SIZE]);
void release_memory(char* words[], int num_words);
void print_words(char* words[], int num_words)
{
int i;
for ( i = 0; i < num_words; i++) {
printf("%s\n", words[i]);
}
}
/*--------------------------------------------------------------------------------------------*/
int main()
{
unsigned char key[KEY_SIZE] = {
'>', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '~', 'S', 'D',
'F', 'G', '*', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', '+', 'M',
'm','b','n','v','z','c','@','q','r','u','i','e','r',
't','o','p','w','$','j','g','d','%','s','f','h','k'
};
int num_words =0, i;
char* words[WORDS_ARRAY_SIZE];
for ( i = 0; i < WORDS_ARRAY_SIZE; i++) {
words[i] = NULL;
}
num_words = read_words(words, WORDS_ARRAY_SIZE, MAX_STR_LEN);
if (num_words == -1)
{
printf("Failed to read words.\n");
return 0;
}
encrypt_words(words, num_words, key);
print_words(words, num_words);
release_memory(words, num_words);
return 0;
}
/*--------------------------------------------------------------------------------------------*/
char encrypt_char(unsigned char key[KEY_SIZE], char ch)
{
int temp=0;
if(ch>='A' && ch<='Z')
{
temp=ch-'A';
}
else if(ch>='a' && ch<='z')
{
temp=ch-ARR_PLACE;
}
return key[temp];
}
/*--------------------------------------------------------------------------------------------*/
int read_words(char* words[], int size, int max_str_len)
{
int counter=0;
int length=0;
char tempWord[max_str_len];
while(counter<size && (scanf("%s", tempWord)!=EOF))
{
length=strlen(tempWord);
words[counter] = (char*)malloc(sizeof(char)*(length)+1);
if(words[counter]==NULL)
{
return -1;
}
strcpy(words[counter], tempWord);
counter++;
continue;
}
return counter;
}
/*--------------------------------------------------------------------------------------------*/
void encrypt_words(char* words[], int num_words, unsigned char key[KEY_SIZE])
{
char tempArr[MAX_STR_LEN];
for(int i=0; i<num_words; i++)
{
strcpy(tempArr, words[i]);
int length=strlen(tempArr);
for(int j=0; j<length; j++)
{
encrypt_char(key, tempArr[j]);
}
strcpy(words[i],tempArr);
}
}
/*--------------------------------------------------------------------------------------------*/
void release_memory(char* words[], int num_words)
{
int i=0;
while(i<num_words)
{
free(words[i]);
i++;
}
}
Upvotes: 1
Views: 111
Reputation: 12669
The function encrypt_char()
returns the encrypted character but you are not assigning it. In the encrypt_words()
you need to do:
tempArr[j] = encrypt_char(key, tempArr[j]);
With this, I am getting the expected output:
$ ./a.out
SILVER
LOSCTK
Upvotes: 1