i am having some problems with a buffer. Short story, i have to iterate over the lines in a text file, in which each line has information separated by an empty space, the problem is, the informartion can have an space in it so i wrote a code that check all the empty spaces of a string and checks if its a sperator, and if it is, ut replaces it by a ";".The problem: I write this to another var in where i use malloc to allocate its space, but it ends printing garbage, can somebody point me what's wrong in the code?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int i;
char *destination;
char* str = "S:15 B:20 B A:15",letra;
destination = (char *)malloc(strlen(str)*sizeof(char));
for (i=0;i<strlen(str);i++){
printf("%c \n",str[i]);
letra = str[i];
if (i == 0){
destination[i] = letra;
}
else if (letra != ' '){
destination[i] = letra;
}
else if (letra == ' ' ){
if (isdigit(str[i-1])){
destination[i] = ";";
}
else{
destination[i] = letra;
}
}
}
printf("%s",destination);
return 0;
}