Thread: Remove a char

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Remove a char

    Im trying to remove a space from an array and for whatever reason
    I get the string doubled when I call the function.
    In the function itself I have the right result.

    Code:
    #include <iostream>
    using namespace std;
    
    void DelChar(char *src)
    {
        char buf[260];
        int i =0;
        while (*(src) != '\0')
        {
            if (*(src) != ' ')
            {
                buf[i] = *(src);
                i++;
            }
            src++;
        }
        buf[i] = '\0';
        strcpy(src,buf);
        cout << "-> " << src << endl;
        return;
    }
    
    int main()
    {
        char text[260] = "he llo";
    
        DelChar(text);
        cout << text << endl;
    
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So which language are you programming in?

    You posted C++ code in the C forum.

    But you're also using messy C-style char arrays rather than std::string.

    > I get the string doubled when I call the function.
    Would that have anything to do with printing it twice?
    Lines 19 and 28
    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.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    I use C code most of the time in C++ code because there are C functions that I prefer to their C++ version.

    Nah, on line 19 I get the correct result and on 28 i get it doubled with the old and the new array.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    You don't actually need an extra char buffer. You can just re use src.

    Code:
    void DelChar(char *src)
    {
        int i = 0;
    
        while (*(src) != '\0')
        {
            if (*(src) != ' ')
    
            {
                src[i] = *(src);
    
                i++;
            }
            src++;
        }
        src[i] = '\0';
    
        cout << "-> " << src << endl;
        return;
    }
    I haven't tested it but it should look like that.
    The price of reliability is the pursuit of the utmost simplicity. - Sir Charles Antony Richard Hoare,

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    C and C++ are two distinct languages and should be treated as such. Avoid writing pure C code in C++ unless there is a good reason to do so. If you don't know enough C++ to use the std:string and other nice utilities then program in C, and compile with a C compiler.

    EDIT: Why in God's good name would you use C-style string manipulation in C++ is beyond me, when std has a lot of user friendlier string manipulation utilities.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    You are getting double because you are passing src which now points to '\0' after "he llo" when you pass it to strcpy which will result to "he llohello".
    The price of reliability is the pursuit of the utmost simplicity. - Sir Charles Antony Richard Hoare,

  7. #7
    Registered User
    Join Date
    Mar 2012
    Location
    Bucharest, Romania
    Posts
    5
    By the time you call strcpy(src, buf), you've already moved src (as in: src - the pointer to the beginning of the string) to the string's actual end by incrementing src for each valid character. Therefore, str points to the end of the string you want to copy when you call strcpy and the strings are concatenated. You get the result you want in the function body because of the location of the str pointer. However, this change on the pointer value is not preserved upon returning from the function only the changes to the memory itself, so in main str will point where it initially did, but the characters you accidentally appended to it are still there.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    @mnd22 Thank you, you are right it should work without extra buffer.
    Its not working for the moment but im tryting to find out why.
    "passing src which now points to '\0' after "he llo" when you pass it to strcpy" exactly, I see it now.

    @claudiu They are not two distinct languages otherwise why can you use C in C++?
    I prefer cout to printf for example. Also C to C++ is like WinAPI to MFC, you see more whats happening in C then in C++.
    Using Windows 10 with Code Blocks and MingW.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    @aleris Thank you, very clear explanation.
    Using Windows 10 with Code Blocks and MingW.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    @mnd22 The problem with this solution is that src[i] gets incremented two times each time.
    Once because of i++ and again because of src++.
    Here is the solution:
    Code:
            if (*(src) != ' ')
            {
                src[i] = *(src);
            }
            else i--;
    :-)
    Last edited by Ducky; 03-19-2012 at 09:43 AM.
    Using Windows 10 with Code Blocks and MingW.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Ducky View Post
    @mnd22 Thank you, you are right it should work without extra buffer.
    Its not working for the moment but im tryting to find out why.
    "passing src which now points to '\0' after "he llo" when you pass it to strcpy" exactly, I see it now.

    @claudiu They are not two distinct languages otherwise why can you use C in C++?
    I prefer cout to printf for example. Also C to C++ is like WinAPI to MFC, you see more whats happening in C then in C++.
    You are dead wrong I am afraid. You are making the costly mistake of learning C++ from the perspective of a C developer rather than as a new language. This is a mistake that C developers often make.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > @claudiu They are not two distinct languages otherwise why can you use C in C++?
    No you're wrong.
    They share a common heritage, but you can't take EVERY valid C program and immediately compile it as a valid C++ program.

    For example, I can do
    int class;
    in C, and it all works just peachy.
    But it would just blow up in C++.

    Read this for many more traps and pitfalls.
    Incompatibilities Between ISO C and ISO C++

    If you compile it with a C++ compiler, then it IS C++. Any C syntax you think you're using will be compiled according to C++ rules.
    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.

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    lol told you its not tested but you got the idea.
    The price of reliability is the pursuit of the utmost simplicity. - Sir Charles Antony Richard Hoare,

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    @Claudiu & Salem
    Thanks I will try to lose the bad habit then.
    But I hope a bit of char array manipulation cant cause much harm in a C++ code, cant it?
    Or to call strcpy() and such functions in a C++ code?
    Using Windows 10 with Code Blocks and MingW.

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    There is no need for such functions in C++. std::string is infinitely superior to C strings, which are really just char arrays. The C++ standard library makes string manipulation seamless by providing a lot of built in functionality. In C you are reinventing the wheel by writing your own expensive function to perform the trivial task of removing a character.
    Check string - C++ Reference and the C++ forum to get a better idea how proper C++ is written. I don't want to post more here since this is after all, the C forum and nobody is required to understand C++.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Remove a specific char and remove multiple spaces
    By stam in forum C++ Programming
    Replies: 9
    Last Post: 12-18-2010, 07:50 AM
  2. Remove newline char at the end
    By Ducky in forum C++ Programming
    Replies: 9
    Last Post: 12-23-2009, 02:29 PM
  3. Remove whitespaces from char*
    By desmond5 in forum C Programming
    Replies: 17
    Last Post: 03-10-2008, 11:39 AM
  4. remove(const char* ) only with extension?
    By mosu' in forum C++ Programming
    Replies: 4
    Last Post: 10-17-2006, 01:35 PM
  5. Help! Remove a char in a string
    By jkert in forum C Programming
    Replies: 3
    Last Post: 10-07-2003, 01:20 PM