1

I want to encode a string in C using a key string. The encodeMSG function returns int array, so that:
intArr[i] = the int value of msg[i] + the int value of key[i].
If the length of the key string is shorter the the msg string, it should go back to the beginning (cycle).

I'm not sure how I should do this although it doesn't seem too complicated.
I also wasn't sure whether I should use atoi(msg + i) or a simple cast like (int)(*(msg + i)).

int *encodeMSG(char *msg, char *key)
{
   int i, msgLen;
   int *encodedArr = (int *)malloc(strlen(msg) * sizeof(int));
   char *keyBackup = key;
   msgLen = (strlen(msg));

   for (i = 0; i < msgLen; ++i)
   {
       if (*(key + i) == '\0')
           key = keyBackup;

       *(encodedArr + i) = *(msg + i); //creating an integer-represented array of the char array [msg]
       *(encodedArr + i) += *(key + i); //adding the [key] array integer values to the integer-represented array of the message
   }

   return encodedArr;
}
tempy
  • 897
  • 3
  • 14
  • 22

3 Answers3

1

Try this:

int *encodeMSG(char *msg, char *key)
{
    char *key0 = key;
    int *result = (int*) malloc( sizeof(int)*(strlen(msg)+1) );
      // +1 to include the final zero of msg
    int *result0 = result;
    while( *msg ) // i assume it's zero-terminated
    {
        *result++ = ((int)*msg) + ((int)*key);
        ++msg;
        ++key;
        if(!*key)key=key0; // reset it to the beginning of the key
    }
    *result = 0; // the final zero
    return result0;
}

This will use the ascii values directly, atoi is not useful here. Note that *msg is the actual char msg points to. As msg is zero-terminated, the end of the string is 0 of false. Same applys for *key and when *key == 0 (or !*key) then just reset it to the start of the key phrase.

By the way: It returns an int[] with 1 more int than chars in msg, last int is zero.

max.haredoom
  • 848
  • 8
  • 7
  • 1
    Thanks for your help, this is a better approach. It's also possible to give up the `key0` backup if using a for loop and the remainder trick as suggested by hijindal: `key[i%key_len]`. – tempy Oct 06 '12 at 14:33
  • Sorry there's a bug in my src... after »int *result = (int*) malloc( sizeof(int)*(strlen(msg)+1) );« put a »int *result0 = result;« and »return result0;« at the end or you would get a pointer to the last item of result... – max.haredoom Oct 06 '12 at 15:00
  • You can edit your answer and fix the problem, that will help others in the future! – Karen Butzke Oct 06 '12 at 15:33
  • [Don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169), and you also need to increment `result` in the loop or you just write to the same location all the time. – unwind Oct 06 '12 at 18:55
  • Your're right... i'll correct that ;-) Why shouldn't i cast the return of malloc. It's just to get rid of warnings (malloc returns void*) – max.haredoom Oct 06 '12 at 23:04
0

If you are looking for ASCII encoding then, just use:

for(i=0; i<msglen; ++i)
  res[i] = msg[i] + key[i%key_len]

However if your character contains a digit, you may simply get its integer value as

int a = ch - '0'    // if ch = '5' then value of a comes out to be 5

if you don't subtract '0', then value of a would have been 53 (ASCII value of '5')

hjindal
  • 588
  • 7
  • 16
  • I now realized what atoi does, which is not what I want. I am looking for ASCII encoding, however, there must be something wrong with my code, since my int array doesn't get any values, even when I do a simple char to int cast to get the ASCII value of the char. – tempy Oct 06 '12 at 14:11
  • if you want ascii values, try for loop which i wrote in the post, that will surely work... – hjindal Oct 06 '12 at 14:14
  • What's the purpose of the `i%key_len` in your loop? – tempy Oct 06 '12 at 14:20
  • 1
    key_len = strlen(key)-1; if key length is shorter than the message length, then i%key_len will automatically move to beginning of the key... – hjindal Oct 06 '12 at 14:22
0

shorter version:

int *encodeMSG(char *msg, char *key)
{
  int *result = (int*) malloc( sizeof(int)*(strlen(msg)+1) );
  int key_len = strlen(key);

  for(size_t i=0, msglen=strlen(msg); i<msglen; ++i )
     result[i] = msg[i] + key[i % key_len];

   result[i] = 0; // the final zero
   return result;
 }
hjindal
  • 588
  • 7
  • 16