I am new programmer in C. I meet a difficult when I write a code " insert a string into a string".
For example:
I have a string: "she is 21 old"
I want to insert "years" between "21" and "old". (position is 10)
Output is : she is 21 years old.
This is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char position, str[100],substr[20];
int i, count ;
printf("-------------------------\n");
printf("Enter a sentence (str): ");
gets(str);
printf("\n Insert a word (substr): ");
gets(substr);
printf("\n Insert word at position (i): ");
gets(i);
for (count = 0; count < strlen(str)-1; count ++)
{
if (count == i)
{
strcat(str,substr);
printf("%s\n", str);
}
}
return 0;
}
when i compile this code, I recieved a warning about gets function. And when i run it, after i enter 2 strings and position. I recieved an error: segmentation fault (core dumped)
Please help me how to fix it, thank so much.
Tran