Question

I am creating a shared object which filters the alphanumeric characters it receive from char* buff_in and copy them into char *buff_out, right now my code is doing what I want but it creates ^@ in between the digits.

int tratar(char* buff_in, char* buff_out){
 int i = 0;

while(buff_in[i]){
 if(!isalpha(buff_in[i])){
    buff_out[i] = buff_in[i];
   }
  i++;
 }
 printf("%s", buff_out);
}  

If I run the program everything looks OK, but when i look at the returned value in a editor I see: ^@^@^@ ^@^@ 1 ^@^@^^@ ^@ 10 ^@^@^@ 50 ^@^@^@. when it should be only 1 10 50 .

Thank you

Was it helpful?

Solution

You need a different index for buff_out:

int i = 0, j = 0;
...
buff_out[j++] = buff_in[i];

Also, null-terminate buff_out before printf():

buff_out[j] = '\0';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top