str_1
and str_2
are arrays of char and the way you fill in the data is fine. But when you are using puts()
method, you have to mark the end of the string by '\0'
character as described in this link http://www.cplusplus.com/reference/cstdio/puts/
The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This terminating null-character is not copied to the stream.
Thus you have to add one more place in the str_1
and str_2
for the null-terminating character:
char str_1[6], str_2[6], my_str[] = "hello world";
Then after copying the substrings into those arrays, put '\0' right after the last copied character like this:
//storing hello in str_1.int i;for (i = 0; i <= 4; ++i) { str_1[i] = my_str[i];}str_1[i] = '\0';