Thread: how to remove a character from a string

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    21

    how to remove a character from a string

    Hello,
    my problem is quite simple to explain.
    I have the following string:

    "table+camera"

    and I want to remove the + sign:
    "tablecamera".

    How do i do that ?
    Thanks for your help.

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    char *p = string;
    int ofs = 0;

    while(*(p+ofs)) {
    while(*(p+ofs) == '+') ++ofs;
    *p = *(p+ofs); ++p;
    } *p = 0;

    fixing the many errors i have made in this code might be a good exercise
    .sect signature

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    21
    thanks.
    Isn't there another way to do that using a combination of the String functions provided by the C ANSI ?
    like strstr(), strtok() ...

    thanks

  4. #4
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    if you have a single +, then you can use strstr(string, "+") to find a point in the string w/ a + and then use strcpy to write all the characters past this point back one, but if you have multiple characters to replace rolling your own replacement function to do it in a single pass might be worth it
    .sect signature

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    21
    can you write the code in details ?
    I do not have a compiler at the moment, so I cannot make tests for now.
    Thanks

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Since + is a CHARACTER and not a string you should use strchr not strstr.

    Code:
    #include <string.h>
    #include <stdio.h>
    
    
    int main (void)
    {
      char str[30]="H+e+l+l+o+W+o+r+l+d";
      char dest[30]="\0";
      char *front, *end;
    
      front = str;
    
      while ( (end = strchr(front, '+')) != NULL)
      {
        strncat(dest, front, end-front);
        front = end + 1;
      }
      /* Take care of the rest of the string */
      end = front + strlen(front);
      strncat(dest, front, end-front);
    
      puts(dest);
      return 0;
    }
    Note: since I'm using strncat you have to put the null character when you declare the destantion.

    Hopefully this will give you enough information to modify it to suit your needs.

  7. #7
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    strchr, strstr, bah
    .sect signature

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    21
    thanks, that is exactly what i needed

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Also here...

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    //hi try this one
    #include <string.h>
    #include <stdlib.h>
    #define NULL 0
    main()
    {
    char string[]="table+camera";
    char seps[]= "+";
    char* token1;//found by strtok
    char* token2;
    token1 = strtok(string,seps);
    token2 = strtok(NULL,seps);
    strcat(token1,token2);
    //look in token1
    }

    by the way while i was trying to create string as char* i got some breaks, whats the problem

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > #define NULL 0
    Include the correct header file, don't define NULL in your own code

    > main()
    Whilst its much better than saying void main, saying int main() is preferred. Especially since C99 (the latest C standard) makes implicit types illegal, it's best to get into the habit of being specific.

    > strcat(token1,token2);
    Bad news - strcat() had undefined behaviour if the source and destination strings overlap (as they do in this case).

    > by the way while i was trying to create string as char* i got some breaks
    Most modern compilers on most modern operating systems make string "constants", really constant, by placing them in read-only memory. Since strtok modifies the string by writing \0 into it, your OS will step in and kill your program as soon as you try it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    void remove_character(char * dst, char * src, char ch)
    {
    	char chCur;
    	size_t iSrc = 0, iDst = 0;
    
    	do
    	{
    		if ((chCur = src[iSrc++]) != ch) dst[iDst++] = chCur;
    	}
    	while (chCur != '\0');
    }
    
    
    
    int main(void)
    {
    	char tst[] = "++++ta+ble+cam+era++++";
    	char tst2[sizeof(tst)];
    
    	/* To another buffer... */
    	puts(tst);
    	remove_character(tst2, tst, '+');
    	puts(tst2);
    
    	/* and in place... */
    	puts(tst);
    	remove_character(tst, tst, '+');
    	puts(tst);
    
    	getchar();
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM