Delete Characters from char array

Discussion in 'C++' started by Michael R. Copeland, Nov 8, 2005.

  1. What is a good way to delete 1 character (or several) from a
    character array that's being processed as a "string"? Specifically, I
    have something like:
    char szWork[128];
    ....
    strcpy(szWork, "-1234.5"); // populate the data
    At this point, I'd like to remove the '-' that's in the 1st character
    position (and this example isn't realistic), and I can't see a good way
    to do this: strcpy with a char pointer that points to the 2nd character;
    memmove; I don't know...
    Furthermore, it might be desireable to remove more than 1 character -
    and from within the string's contents. I'd want to extend the
    particular problem above to do that, as well.
    This seems such a common operation (editors, data conversions, etc.),
    that I find it odd I can't find examples (on google, etc.) of how to do
    this.
    I suppose I could use std::string or some advanced "string"
    processing (and I don't know what to use there, either), but that seems
    pretty extreme for this.
    Any thoughts? TIA...
    Michael R. Copeland, Nov 8, 2005
    #1
    1. Advertising

  2. Michael R. Copeland

    John Ratliff Guest

    Michael R. Copeland wrote:
    > What is a good way to delete 1 character (or several) from a
    > character array that's being processed as a "string"? Specifically, I
    > have something like:
    > char szWork[128];
    > ...
    > strcpy(szWork, "-1234.5"); // populate the data
    > At this point, I'd like to remove the '-' that's in the 1st character
    > position (and this example isn't realistic), and I can't see a good way
    > to do this: strcpy with a char pointer that points to the 2nd character;
    > memmove; I don't know...
    > Furthermore, it might be desireable to remove more than 1 character -
    > and from within the string's contents. I'd want to extend the
    > particular problem above to do that, as well.
    > This seems such a common operation (editors, data conversions, etc.),
    > that I find it odd I can't find examples (on google, etc.) of how to do
    > this.
    > I suppose I could use std::string or some advanced "string"
    > processing (and I don't know what to use there, either), but that seems
    > pretty extreme for this.
    > Any thoughts? TIA...


    I don't think a std::string is too "extreme" for this. I think it's a
    perfect demonstration of std::string as a better "C string".

    I would use std::string myself.

    --John Ratliff
    John Ratliff, Nov 8, 2005
    #2
    1. Advertising

  3. > > What is a good way to delete 1 character (or several) from a
    > > character array that's being processed as a "string"? Specifically, I
    > > have something like:
    > > char szWork[128];
    > > ...
    > > strcpy(szWork, "-1234.5"); // populate the data
    > > At this point, I'd like to remove the '-' that's in the 1st character
    > > position (and this example isn't realistic), and I can't see a good way
    > > to do this: strcpy with a char pointer that points to the 2nd character;
    > > memmove; I don't know...
    > > Furthermore, it might be desireable to remove more than 1 character -
    > > and from within the string's contents. I'd want to extend the
    > > particular problem above to do that, as well.
    > > I suppose I could use std::string or some advanced "string"
    > > processing (and I don't know what to use there, either), but that seems
    > > pretty extreme for this.
    > > Any thoughts? TIA...

    >
    > I don't think a std::string is too "extreme" for this. I think it's a
    > perfect demonstration of std::string as a better "C string".
    >
    > I would use std::string myself.


    Fair enough. How do I do it to solve the problem(s)? What methods
    do I use, and how do I use them?
    Michael R. Copeland, Nov 8, 2005
    #3
  4. Michael R. Copeland

    Ian Guest

    Michael R. Copeland wrote:
    > What is a good way to delete 1 character (or several) from a
    > character array that's being processed as a "string"? Specifically, I
    > have something like:
    > char szWork[128];
    > ...
    > strcpy(szWork, "-1234.5"); // populate the data
    > At this point, I'd like to remove the '-' that's in the 1st character
    > position (and this example isn't realistic), and I can't see a good way
    > to do this: strcpy with a char pointer that points to the 2nd character;
    > memmove; I don't know...
    > Furthermore, it might be desireable to remove more than 1 character -
    > and from within the string's contents. I'd want to extend the
    > particular problem above to do that, as well.
    > This seems such a common operation (editors, data conversions, etc.),
    > that I find it odd I can't find examples (on google, etc.) of how to do
    > this.
    > I suppose I could use std::string or some advanced "string"
    > processing (and I don't know what to use there, either), but that seems
    > pretty extreme for this.
    > Any thoughts? TIA...


    Yes, use a std::string, it has all that you require for this.

    Ian
    Ian, Nov 8, 2005
    #4
  5. Michael R. Copeland

    Jon Bell Guest

    In article <>,
    Michael R. Copeland <> wrote:
    [somebody else wrote:]
    >> > What is a good way to delete 1 character (or several) from a
    >> > character array that's being processed as a "string"? Specifically, I
    >> > have something like:
    >> > char szWork[128];
    >> > ...
    >> > strcpy(szWork, "-1234.5"); // populate the data
    >> > At this point, I'd like to remove the '-' that's in the 1st character

    >>
    >> I would use std::string myself.

    >
    > Fair enough. How do I do it to solve the problem(s)? What methods
    >do I use, and how do I use them?


    #include <iostream>
    #include <string>
    using namespace std;

    int main ()
    {
    string szWork = "-1234.5";
    szWork.erase(0,1); // starting at position 0, delete 1 character
    cout << szWork << endl;
    return 0;
    }

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Nov 8, 2005
    #5
  6. Michael R. Copeland

    Guest

    > This seems such a common operation (editors, data conversions, etc.),
    > that I find it odd I can't find examples (on google, etc.) of how to do
    > this.


    It's just too simple ;-). std::string can do it for you, as well as
    many other things. Consult the standard library documentation.

    The easy way (easy for small things, hard for more complicated), e.g.
    to remove the minus sign (without substituting it by something else):

    char szWork[128];
    strcpy (szWork, "-1234.5");
    //...
    szWork++;

    This just increases the position of the first character. To substitute
    the first character by a '+':

    *szWork = '+'; // szWork == "+1234.5"

    To substitute the 2nd character by a '2':

    *(szWork+1) = '2'; // szWork == "+2234.5"

    And so on. But remember - std::string gives more elegant solutions and
    more possibilities, especially when things get more complicated.


    Eckhard
    , Nov 8, 2005
    #6
  7. wrote:
    >> This seems such a common operation (editors, data conversions, etc.),
    >> that I find it odd I can't find examples (on google, etc.) of how to do
    >> this.

    >
    > It's just too simple ;-). std::string can do it for you, as well as
    > many other things. Consult the standard library documentation.
    >
    > The easy way (easy for small things, hard for more complicated), e.g.
    > to remove the minus sign (without substituting it by something else):
    >
    > char szWork[128];
    > strcpy (szWork, "-1234.5");
    > //...
    > szWork++;
    >


    Er. szWork is not a modifiable lvalue.

    Krishanu
    Krishanu Debnath, Nov 8, 2005
    #7
  8. Michael R. Copeland

    Guest

    >> char szWork[128];
    >> strcpy (szWork, "-1234.5");
    >> //...
    >> szWork++;

    >
    >Er. szWork is not a modifiable lvalue.


    Ough, sorry - you're right.

    It needs to be an dynamically allocated char * if it should be
    manipulated:

    char *szWork = new char[128];
    //...
    delete [] szWork;


    Eckhard
    , Nov 8, 2005
    #8
  9. Michael R. Copeland

    John Ratliff Guest

    wrote:
    >>>char szWork[128];
    >>>strcpy (szWork, "-1234.5");
    >>>//...
    >>>szWork++;

    >>
    >>Er. szWork is not a modifiable lvalue.

    >
    >
    > Ough, sorry - you're right.
    >
    > It needs to be an dynamically allocated char * if it should be
    > manipulated:
    >
    > char *szWork = new char[128];
    > //...
    > delete [] szWork;
    >
    >
    > Eckhard
    >


    You could also make a char* to szWork and work off that.

    char szWork[128];
    char* ptr = szWork;
    strcpy(szWork, "-1234.5");
    // blah blah
    ++ptr; // *ptr == "1234.5"

    --John Ratliff
    John Ratliff, Nov 8, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    546
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,034
    Ian Collins
    May 9, 2006
  3. Replies:
    3
    Views:
    731
  4. davidb
    Replies:
    0
    Views:
    759
    davidb
    Sep 1, 2006
  5. davidb
    Replies:
    6
    Views:
    1,544
    Default User
    Sep 1, 2006
Loading...

Share This Page