Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3229558
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:49:28+00:00 2026-05-17T16:49:28+00:00

How can I split up a string into pieces? For example, how can I

  • 0

How can I split up a string into pieces? For example, how can I place “orld.” into a variable called one, “Hello” into a variable called three, and ” w” into two?

#include <string.h>
#include <stdio.h>

int main(void)
{
    char *text ="Hello World."; /*12 C*/

    char one[5];
    char two[5];
    char three[2];

    return 1;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:49:29+00:00Added an answer on May 17, 2026 at 4:49 pm

    For one, you cannot do what you asked, and still have them work as null-terminated strings. This is because the memory layout of text looks like this:

    char text_as_array[] = {
      'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '.', '\0'
    };
    

    Notice the '\0' at the end. The character array test is not actually length 12, it is length 13.

    Null-termination is required for console output functions, like printf and std::cout (C++) to work:

    char good[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
    printf("%s\n", good); /* this works, because it is null-terminated */
    
    char bad[] = { 'H', 'e', 'l', 'l', };
    printf("%s\n", bad); /* this will print out garbage, or crash your program! */
    

    This means that you must define your arrays like this:

    char one[6];
    char two[3];
    char three[6];
    

    You can do this by hand by simply copying the values out:

    one[0] = text[7]; /* o */
    one[1] = text[8]; /* r */
    one[2] = text[9]; /* l */
    one[3] = text[10]; /* d */
    one[4] = text[11]; /* . */
    one[5] = '\0'; /* you have to null-terminate the string */
    

    If you want to do less typing, or simply want to write better code, you can take advantage of the fact that arrays/strings are contiguous, and use a loop to copy that data over:

    for(int i = 0; i < 5; ++i)
    {
      one[i] = text[7 + i];
    }
    one[5] = '\0';
    

    But if you guess that this is a really common thing to do in C, then you guess right. Instead of manually coding this loop each time, you should use a built-in function to do the copying for you:

    /* C uses "", C++ uses <> */
    #include "string.h" /* C++: #include<cstring> */
    #include "stdio.h" /* C++: #include<cstdio> */
    
    /* don't do "int function(void)", do "int function()" instead */
    int main()
    {
      char *text = "Hello World."; /* string length 12, array size 13 */
    
      /* size of these has to accomodate the terminating null */
      char one[6];
      char two[3];
      char three[6];
    
      /* copy two characters, starting at index 5 */
      strncpy(two, &text[5], 2);
    
      /* for three, we don't have to do &text[0], because it is at the beginning of the string */
      strncpy(three, text, 5);
    
      /* we can do strcpy if we're at the end of the string.  It will stop when it hits '\0' */
      strcpy(one, &text[7]);
    
      /* note that we still have to null-terminate the strings when we use strncpy */
      two[2] = '\0';
      three[5] = '\0';
    
      /* but we don't have to null-terminate when we use strcpy */
      /* so we can comment this out: one[5] = '\0'; */
    
      printf("%s\n", one);
      printf("%s\n", two);
      printf("%s\n", three);
    
      return 0; /* returns 0, since no errors occurred */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the String.Split() method in C#. How can I put the resulting
Using SQL Server, how do I split a string so I can access item
What is a good way in Perl to split a line into pieces of
How do I split a string into a multidimensional array or a jagged array
I want to know how i can split a string in php based on
i'm able to split a string into an array $array = preg_split(/[\s]*[,][\s]*/, $category); RESULT
I can split editor panes horizontally or vertically, but it does not seem possible
How can I split long commands over multiple lines in a batch file?
How can I split by word boundary in a regex engine that doesn't support
Can you cast a List<int> to List<string> somehow? I know I could loop through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.