I wrote this code:

Code:
char* remove_WS(char* input)
{
  char *temp, *temp2;
  unsigned int counter = 0;
  int original_length = strlen(input), i;
  printf("ORIGINAL INPUT LENGTH: %d\n", original_length);
  temp = malloc(sizeof(char)*original_length);
  for(i = 0; i < original_length; i++)
    {
      if (input[i] == ' ' || input[i] == '\n')
    continue;
      else
    {
      printf("Char recorded %d: %c\n",i,input[i]);
      counter++;
      temp[i] = input[i];
    }
    }
  printf("temp after loop: %s\n", temp);
  printf("temp STRLEN after loop: %d\n", strlen(temp));
  temp2 = realloc(temp,strlen(temp));
  printf("temp2 strlen: %d\n", strlen(temp2));
  return temp2;
}
The problem I am having is that somehow, there is junk being inserted into the char* and I have no idea how/why, given the output from the code:


Code:
ORIGINAL INPUT LENGTH: 12
Char recorded 0: Z
Char recorded 1: e
Char recorded 2: l
Char recorded 3: l
Char recorded 4: o
Char recorded 6: W
Char recorded 7: o
Char recorded 8: r
Char recorded 9: l
Char recorded 10: d
Char recorded 11: !
temp after loop: Zello+World!)\MiΘÑP
temp STRLEN after loop: 22
temp2 strlen: 17
You can sorta see my feeble debugging attempts here, and I am stumped. Clearly, the iteration is only going through 12 times because Char recorded only happens 12 times total. However, the char* temp that the loop has stored the characters in is length 22???? That's where I'm lost. I've also tried using counter in realloc instead of strlen but no change. Thank you!

Btw, sorry for the 'Z' this function is part of a bigger program that I am writing to work with strings and I had earlier used a different function to change the H to a Z!