Apologies if the question has already been answered before. I could not find any help.
My assignment is to create a function that takes a structure (which contains a string) in parameter and replace a character in that string. Right now I'm trying to replace spaces with the letter 'J' but I'm having some difficulty.
Heres my code:
func.h
typedef struct item item;
struct item {
char * word;
};
void space(item * item, char letter);
func.c
void space(item * item, char letter) {
char * mot = item->word;
int size = strlen(item->word);
printf("\n\n%s\n\n",mot);
for(int i=0; i<=size; i++) {
if(mot[i] == ' ') {
mot[i] = letter;
}
}
printf("\n\nNEW WORD: ");
printf("%s",mot);
printf("\n\n");
}
main.c
int main(int argc, char *argv[]) {
printf("\n---Program---\n\n");
char *word;
char add = 'x';
item * item = malloc(sizeof(item));
item->word = "this is a word";
//printf("%s",item->word);
printf("\n\n");
space(item,add);
return 0;
}
Here's the error I'm getting:
Seg fault!!!