Thread: deleting invaid characters in a string

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    deleting invaid characters in a string

    How do I deleting invaid characters in a string?

    eg. the user types in hello.daniel is there a way to delete the . in the middle of hello and daniel to make the string look like this "hellodaniel"

    Thanks
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Sounds like a good exercise in string manipulation to me There isn't a standard function to do this for you, so you'll have to write your own.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    Ok...

    Ok I will start trying to do that, but if anyone comes up with a way to do it can they please post it here

    Thanks
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  4. #4
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Pretty easy.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void remove_char( char *string, const char item )
    {
            char *at = strchr( string, item );
    
            while( *at ) *at++ = *(at + 1);
    }
    
    int main()
    {
            char str[50] = "hello.daniel";
    
            printf( "%s\n", str );
            remove_char( str, '.' );
            printf( "%s\n", str );
    
            return 0;
    }
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  5. #5
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Here's another that removes all occurances of the item instead of just the first one.
    Code:
    void remove_char( char *string, const char item )
    {
            char *at;
            
            while( (at = strchr( string, item )) )
                    while( *at ) *at++ = *(at + 1);
    }
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    Thanks

    I was just about to ask how I would do that

    Thank you to all who replied
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  7. #7
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Ignore the first two functions I gave you, they're both big time wrong. This one's right.
    Code:
    void remchr( char *string, const char item )
    {
            char *at;
    
            while( (at = strchr( string, item )) ) {
                    while( *at ) {
                            *at = *(at + 1);
                            at++;
                    }
            }
    }
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    why?

    Why were the last two functions "big time" wrong? They worked perctly fine for me!
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    also

    When I try to make this function remove a '\' from the string it doesn't work can some please help

    thanks
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  10. #10
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> When I try to make this function remove a '\' from the string it doesn't work can some please help

    Are u trying to remove a '\' ??

    Shouldn't u be trying to remove a '\\' ??

    Try it, maybe it'll work!

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    Yep

    Yes that worked thanks.
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  12. #12
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    The big time problem was in this line.
    Code:
    *at++ = *(at + 1);
    It's undefined behavior because it changes at more than once in the same expression. It worked fine for me too, but that doesn't mean it'll work all the time or even any of the time. You just can't, tell, so it's wrong.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Replies: 4
    Last Post: 10-14-2005, 12:53 PM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM