Eddie Kusturica Eddie Kusturica - 4 months ago 16
C Question

String between program faults

I'm having a bit of an issue with my program. The way it works is that a function is supposed to return a string of char's between two char's that I choose, for example "a1234a" and I want to return all characters between the 'a', I'd get 1234. The problem is that my output also prints the first 'a', so I get "a1234" instead. The code is as following:

char* string_between(char *s, char c){
int length = strlen(s);
int counter = 0;
char* strBetween = malloc(strlen(s));

for(int i = 0; i<length; i++){
if(s[i] == c){
if(counter == 1){
strBetween[i] = s[i];
return NULL;
return strBetween;

I've tried adding
s[i] != c
in the
if(counter == 1)
statement, but then it gets worse and doesn't print anything at all. I'm still pretty new to C so please explain as simple as possible :) Thanks for reading!


There are two problems with this implementation.

First, char* strBetween = malloc(strlen(s)); does not allocate memory for the null-terminating character, assuming your goal here is to allocate enough space to hold a string with length equal to that of the input string. Second, if you reason through the for loop, you'll see what it actually does is copy the character c plus the string of interest (e.g. "a1234") into the string you allocated beginning at the index it was found in the input string.

In the case of "a1234a" as the input string, it will copy over "a1234_" (with the final character being undefined). However, suppose the input string were "ggga1234aggg". The output would be the string "___a1234____" with the "_" characters being undefined. The reason adding s[i] != c in the if condition doesn't work is that you're still not addressing the undefined character(s) at the beginning of the string. In that case, you still end up with "_1234-" (using - instead of _ to avoid italicization). Most likely, the undefined characters will be 0 (i.e. null-terminator), in which case it is essentially a string of length zero, which explains your result. But of course, you should not be relying on undefined behavior regardless.