i am writing a C program and one of the issues i have is to extract a word between two words as below.
ac_auto_lvalue[] =
"ONLY / GROUP: OTHERS EXAMPLE /-----------------------------";
I need to extract the word between “Group:” and the “/”, the two words (Group:” & “/”) will always be there but the words in between can change and in some cases there might be nothing… ( in the above example output would be “OTHERS EXAMPLE”
can anyone help me with a C snippet for the above?
Take a look at the
strstrfunction. It lets you find a pointer to the first occurrence of a specific string (say,"Group:") inside another string. Once you have two pointers (to the beginning and to the end of your string) you can allocate enough memory usingmalloc(don’t forget the terminating zero'\0'), usememcpyto copy the characters, and finally zero-terminate your string.