I am trying to split string in two parts using space as a separator. I had tried the below code but i got wrong result.
#include <stdio.h>
#include <string.h>
void main()
{
char str_1[5], str_2[5], my_str[] = "hello world";
//storing hello in str_1.
for (int i = 0; i <= 4; ++i)
str_1[i] = my_str[i];
puts(str_1);
//storing world in str_2.
for (int i = 6, j = 0; i <= 10; ++i, ++j)
str_2[j] = my_str[i];
puts(str_2);
}
Expected output:
hello
world
Getting output:
hello
worldhello
str_1andstr_2are too short, and, you forgot to add string terminators.'\0'i <= 10, j <= 4does in the condition of the for loop?