#include <stdio.h>
void splitString(const char *from, char start, char end, char *into)
{
/* Really first we make sure the output string can be printed by terminating it */
*into = '\0';
/* First find the `start` character */
while (*from && *from++ != start)
;
/* Now we are either at the end of the string, or found the character */
if (!*from)
return; /* At end of string, character not found */
/* Copy the string while we don't see the `end` character */
while (*from && *from != end)
*into++ = *from++;
/* Now terminate the output string */
*into = '\0';
}
int main()
{
char into[8];
const char *from = "this is #string# i want to look for ";
splitString(from, '#' ,'#', into);
printf("from = \"%s\"\n", from
); printf("into = \"%s\"\n", into
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+Cgp2b2lkIHNwbGl0U3RyaW5nKGNvbnN0IGNoYXIgKmZyb20sIGNoYXIgc3RhcnQsIGNoYXIgZW5kLCBjaGFyICppbnRvKQp7CiAgICAvKiBSZWFsbHkgZmlyc3Qgd2UgbWFrZSBzdXJlIHRoZSBvdXRwdXQgc3RyaW5nIGNhbiBiZSBwcmludGVkIGJ5IHRlcm1pbmF0aW5nIGl0ICovCiAgICAqaW50byA9ICdcMCc7CgogICAgLyogRmlyc3QgZmluZCB0aGUgYHN0YXJ0YCBjaGFyYWN0ZXIgKi8KICAgIHdoaWxlICgqZnJvbSAmJiAqZnJvbSsrICE9IHN0YXJ0KQogICAgICAgIDsKCiAgICAvKiBOb3cgd2UgYXJlIGVpdGhlciBhdCB0aGUgZW5kIG9mIHRoZSBzdHJpbmcsIG9yIGZvdW5kIHRoZSBjaGFyYWN0ZXIgKi8KICAgIGlmICghKmZyb20pCiAgICAgICAgcmV0dXJuOyAgLyogQXQgZW5kIG9mIHN0cmluZywgY2hhcmFjdGVyIG5vdCBmb3VuZCAqLwoKICAgIC8qIENvcHkgdGhlIHN0cmluZyB3aGlsZSB3ZSBkb24ndCBzZWUgdGhlIGBlbmRgIGNoYXJhY3RlciAqLwogICAgd2hpbGUgKCpmcm9tICYmICpmcm9tICE9IGVuZCkKICAgICAgICAqaW50bysrID0gKmZyb20rKzsKCiAgICAvKiBOb3cgdGVybW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