-
Text extraction
Hi,
I ahve a long string of which I want to remove certain words....
for example:
string example =" <this> is some <test> data"
I want to remove words with are contained with "<>"...so after my programs runs I would be left with
<"is some data">
does anybody have idea's what function I could use...to remove such text from the string...
any ideas would be great...
thank
-
A for loop and a boolean variable used to control the copying of chars
You set the bool to false when you see a <
You set it to true when you see a >
If it's true, copy the char
-
Try this. Keep in mind, the code will modify the string directly.
Code:
#include <string.h>
void RemoveWords(char *str);
int main(int argc, char* argv[])
{
char str[81];
strcpy(str, "<this> is some <test> data");
RemoveWords(str);
return 0;
}
void
RemoveWords(char *str)
{
#define START_CHAR '<'
#define END_CHAR '>'
char *p;
char *t;
p = str;
t = p;
while (*t) // while we haven't found the end of the string
{
if (*t == START_CHAR)
{
while (*t && *t != END_CHAR) // look for the END_CHAR
{
t++;
}
t++; // skip the END_CHAR
}
*p = *t; // finally, copy the current character
p++;
t++;
if (*t == '\0')
*p = '\0';
}
}