1

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
3
  • 3
    str_1 and str_2 are too short, and, you forgot to add string terminators. Commented May 27, 2020 at 14:40
  • 1
    Your strings are missing string terminators '\0' Commented May 27, 2020 at 14:40
  • Also what do you think i <= 10, j <= 4 does in the condition of the for loop? Commented May 27, 2020 at 14:41

4 Answers 4

1

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';
Sign up to request clarification or add additional context in comments.

Comments

0

You have to terminate the string using \0 using null character as last character of the string. Try the below code.

#include <stdio.h>
#include <string.h>
void main()
{
    char str_1[6], str_2[6], my_str[] = "hello world";

    //storing hello in str_1.
    for (int i = 0; i <= 4; ++i)
        str_1[i] = my_str[i];

    str_1[5] = '\0';
    puts(str_1);

    //storing world in str_2.
    for (int i = 6, j = 0; i <= 10; ++i, ++j)
        str_2[j] = my_str[i];

    str_2[5] = '\0';
    puts(str_2);
}

Comments

0

There is some basic mistakes in the code.Please go through c staring related theories. please go through code and comments.

  • staring should be always end with '\0' (null character).
  • character array size should always +1 of the string.
#include <stdio.h>
#include <string.h>
void main()
{
    /* array side should be (staring size + 1) here 1 is for staring terminator ('\0') */
    char str_1[6], str_2[6], my_str[] = "hello world";
    int i =0, j =0;
    //storing hello in str_1.
    for (i = 0; i <= 4; ++i)
        str_1[i] = my_str[i];
    /* You need to add null terminator ('\0') at the end of the staring so that we can print it properly */
    str_1[i] = '\0';
    puts(str_1);

    //storing world in str_2.
    for (i = 6, j = 0; i <= 10; ++i, ++j)
        str_2[j] = my_str[i];
    /* You need to add null terminator ('\0') at the end of the staring so that we can print it properly */
    str_2[j] = '\0';
    puts(str_2);
}

Comments

0

Another simple example, eg using strtok():

First make sure variables contain enough space for the task:

 //char str_1[5], str_2[5], my_str[] = "hello world";//need minimum of 6 for each 5 char word
 //           ^         ^
 char str_1[6], str_2[6], my_str[] = "hello world";
 //         ^         ^      
 char *tok = strtok(my_str, " ");
 if(tok)
 {
     strcpy(str_1, tok);
     tok = strtok(NULL, " ");
     if(tok)
     {
         strcpy(str_2, tok);//Note - strcpy terminates target with NULL.
     }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.