Thread: Some help with string manipulation

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    18

    Some help with string manipulation

    Hello all. This is my second thread where I've failed to work out string manipulation, and it's fairly similar to my first one. Basically, I've got a string, and I'm looking to remove anything that's placed within angled brackets, as well as the brackets themselves. Here's a snipet of code taken from a function that does what I'm trying to do.

    Code:
    char	newline[512] = {0}, tmp[512] = {0};
    char	*ptr, *ptr2;
    
    while ((ptr = strstr(newline, "<")) != NULL) {
    	while ((ptr2 = strstr(newline, ">")) != NULL) {
    		strcpy(tmp, ptr2);
    		*ptr = 0;
    		strcpy(newline, tmp);
    	}
    }
    Without this code the program runs through fine, but with this, it stalls when it reaches here.
    If anyone needs any more of the code to work it out, I'll post it. Just don't wanna slow you guys down with useless stuff.

    Any help is appreciated

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( (ptr1 = strchr( buf, '<' )) )
    {
        if( (ptr2 = strchr( ptr1, '>' ) )
            memmove( ptr1, ptr2 + 1, strlen( ptr2 + 1 ) + 1 );
        else
        {
            /* Handle "<foo" with no matching ">" how you want. I truncate. */
            *ptr1 = '\0'; 
        }
    }
    There's probably some special case things this won't catch, like nested < > pairs, or mismatched < > sets, etc. Hm... close enough for who it's for.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    18
    Thanks buddy. And don't worry, there are no mismatched ones, and no nested ones either. You really are a live-saver

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:41 AM