Removing single char from string

This is a discussion on Removing single char from string within the C Programming forums, part of the General Programming Boards category; Say I have the string "hello", how would I remove the second 'l' entirely? Would this be right: Code: char* ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Removing single char from string

    Say I have the string "hello", how would I remove the second 'l' entirely? Would this be right:

    Code:
    char* str = "hello";
    
    str[3] = '\0';
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    First of all, string literals aren't guaranteed to be writeable and typically aren't. That program would probably crash with a segmentation fault. Making str an array would fix it.

    However, your method for removing a character would truncate the string entirely at that point so you'd lose the 'o' also. The only way to remove an item from an array is to move everything after that element up one spot in the array. memmove() is well-suited for the task or you can manually loop through.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Yep I just tried it and I didn't work
    Thanks for the help
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Yep I just tried it and I didn't work
    Well then obviously you didn't do it correctly. Post the code you used so we can show you where you went wrong.

    http://www.cplusplus.com/ref/cstring/memmove.html

    edit: For a string this small, you might as well just go through manually. After you call memmove, you have to replace the o with something, otherwise you'll end up with "heloo". It' just as many lines of code to say
    Code:
    str[3] = 'o';
    str[4] = '\0';

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Code:
    	int i = 0;
    	BOOL is_second_l = FALSE;
    	char string[] = "hello";
    
    	while ( string[i] != '\0' )
    	{
    		if ( string[i++] == 'l' && !is_second_l )
    			is_second_l = TRUE;
    
    		if ( is_second_l )
    		{
    			strcpy(string + i, (string + i) + 1);
    			break;
    		}
    	}
    I know, it sucks.. seems redundant and poor but it works.
    Last edited by xeddiex; 09-24-2005 at 12:27 PM.

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I meant the code I posted originally... I made the stupid mistake of not trying it before asking the question.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Say I have the string "hello", how would I remove the second 'l' entirely?
    Code:
    char s[] = "hello", *p = s[3];
    
    while(*p = *(p+1)) p++;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    Code:
    #include <iostream>
    
    void removeChar(char *TheStr, char ch)
    {
         int len = strlen(TheStr), c = 0;
         char Temp1[len], Temp2[len];
         strcpy(Temp1,TheStr);
         for (int x = 0; x< len; x++)
         {
             if (Temp1[x] != ch)
             Temp2[c++] = Temp1[x];
         }
         Temp2[c] = 0;
         strcpy(TheStr, Temp2);
    }

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your code is C99 code only. Meaning it won't compile on a good number of compilers out there.
    Code:
    void removeChar( char *s, char c )
    {
        char *p = NULL;
        for( p = s; p && *p; p++ )
        {
            if( *p == c )
            {
                char *t = NULL;
                for( t = p; *t; t++ )
                    *t = *(t + 1);
            }
        }
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    it compiles fine under dev-cpp & visual studio

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    It's this line here
    Code:
    for (int x = 0; x< len; x++)
    that's causing more of the importability. (And I don't know if you should use len as an array dimension.) It's also pretty ineficient - it uses two buffers, and Quzah's uses none.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Lets not forget the quzah's uses pointers and pointers are your god!

  13. #13
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    im new to c/c++ programming and i havn't learned anything about pointers yet. im still trying to wrap my head around structures.

  14. #14
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Just a thought... what about using memmove() to shift the last part of the string?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    That would work too. Although you couldn't use memcpy(), because the buffers would probably overlap.

    Because memove() allows overlapping buffers, it must copy all the data somewhere and then back again. This is inefficient. Quazh/dwks method with pointers is the best way to do it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21