I was wondering if there is a way I can extract everything before I hit a certain word in a string. The string I have created has been malloced and then populated. I want to create a new string with everything before a certain substring so
String1 = “I need coffee”
how can I extract everything before coffee?
Any ideas?
Thanks
Use
strstrto locate the substring, andstrncpywith a bit of pointer arithmetic (n = startOfPrefix – startOfString) to copy the prefix to a new buffer, don’t forget to null terminate the new string.If you just want to throw away the substring and everything after it you could just set the location returned by
strstrto'\0', to terminate the string there.