1

How do you split a string in two halves using C? I've looked everywhere for the answer but most seem to deal with splitting at spaces or other characters (examples 1, 2, 3). I just want to split a string into 2 halves. Is there a simple way to do this?

5
  • 1
    Sure. Learned math in high school, right? Commented Oct 10, 2013 at 20:52
  • Why can't you find split point using strlen()? Commented Oct 10, 2013 at 20:52
  • 1
    Two halves? "abcde" is to be "abc" and "de" or "ab" and "cde"? (ignoring \n). Commented Oct 10, 2013 at 20:53
  • If you know how long the string is (say, n), then a copy of the substring from indices [0,n/2) is the first half, and &string[n/2] is the second half. Commented Oct 10, 2013 at 20:53
  • ` void split(char *s, char **a, char **b) { int i = strlen(s) / 2; char c = s[i]; s[i] = 0; *a = strdup(s); s[i] = c; *b = strdup(s + i); }` Commented Oct 10, 2013 at 20:57

2 Answers 2

3

No, there's no simple way of doing it - it takes several steps:

  1. Compute the length of the string
  2. Allocate memory for both halves
  3. Copy the content into the first half; add null termination
  4. Copy the content into the second half; add a null terminator
  5. Use your strings
  6. Free the first copy
  7. Free the second copy

Here is how you can do it:

char *str = "quickbrownfox";
int len = strlen(str);
int len1 = len/2;
int len2 = len - len1; // Compensate for possible odd length
char *s1 = malloc(len1+1); // one for the null terminator
memcpy(s1, str, len1);
s1[len1] = '\0';
char *s2 = malloc(len2+1); // one for the null terminator
memcpy(s2, str+len1, len2);
s2[len2] = '\0';
...
free(s1);
free(s2);

If you own the entire string, you do not need to make a second copy, because making a pointer into the middle of a string will just work.

Sign up to request clarification or add additional context in comments.

5 Comments

Here is a demo on ideone.
"there's no simple way of doing it" - Um, I'm sure this is considered pretty simple. I'd say trivial.
@H2CO3 C'mon, spending so many lines of code on a simple pair of substrs is not simple - it's almost like we're back to assembly language programming.
Some say that C is almost like assembly. While I don't agree, I do get the point. My opinion, though, is that this is not what counts as difficult. Creating a complete operating system, writing a 3D engine with heavy maths, coding AI... That's what I call difficult. (I don't say that "German language is hard" just because I don't speak German. Surely I could learn it if I wanted. It's all about effort and practice.)
yea i figured there wouldn't be an easy way like in java or some other language. thanks this helped
1

Not if you want to do it in-place. You'd have to sacrifice one character for the '\0' byte.

You'll need to think about how you want to allocate memory for the 2nd part of the split string, and code accordingly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.