1

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++;
    }
}
9
  • 4
    Read this article for tips on debugging your code. This is a critical skill to learn as you learn to write code. Commented Dec 31, 2019 at 18:02
  • You definitely should learn how to use a debugger, to single step through your code, inspecting all relevant variables to understand what is really going on. Commented Dec 31, 2019 at 18:24
  • Yeah that's on my to do list, whenever I get some free time, but thanks Commented Dec 31, 2019 at 18:26
  • Why did you delete the relevant code? Commented Dec 31, 2019 at 18:27
  • Must've accidentally happened, fixing it. Commented Dec 31, 2019 at 18:28

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

if he is using pointer then why is he even returning that value?? isn't it odd ? He should pass that as pointer
@MaifeeUlAsad: There often are different solutions for one problem.
of course there is.. then why use pointer.. isn't it like eating food without test ??
Thanks, that was the issue indeed :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.