I tried to implement a small splitStringByString() function in C, this is how I have come so far:
char* splitStringByString(char* string, char* delimiter){
int i = 0, j = 0, k = 0;
while(*(string + i) != '\0'){
j = i;
while((*(string + j) == *(delimiter + k)) && (*(string + j) != '\0')){
if(*(delimiter + k + 1) == '\0'){
// return string from here.
char result[(strlen(string) - strlen(delimiter) + 1)]; // + 1 for '\0'
i = 0;
j++;
while(*(string + j) != '\0'){
result[i] = *(string + j);
i += 1;
j++;
}
i = (int)strlen(result);
result[i - 1] = '\0';
return result;
}
k++;
j++;
}
i++;
}
return NULL;
}
So it works more or less; the function returns the string after the delimiter as wanted, but at the end of this string (the last character) is always \377. I already found something that said this is an octal number or so (stackoverflow), but it is not very clear for me. Could you help me and give me some advice about what I did wrong?
Thanks a lot! :-)