strip commas from string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ineedyourluvin1@yahoo.com

    strip commas from string


    Hello all! I've been looking for a way to strip characters from strings
    such as a comma. This would be great for using a comma
    as a delimiter. I show you what I have right now.


    #include<iostre am>
    #include<string >

    int main(int argc, char *argv[])
    {
    using namespace std ;

    char people[14] = "Me,Myself, I" ;
    char nocommas[14] ;
    int i, j ;
    i=j=0 ;





    for(i = 0; i < 14; i++){
    if(people[i] != ',')
    nocommas[i] = people[i] ;

    if(people[i] == ',')
    nocommas[i] = ' ' ;
    }



    cout << "With commas : " << people << endl ;
    cout << "Without commas : " << nocommas << endl ;

    cout << "\n\n\n\n" ;
    cout << "\tNow tell me how I can store each name in a separate
    variable ?" << endl ;
    cout << "\tAnd can someone show me how to do this with string instead
    of char[] ?" << endl ;
    cout << "\t\t Thanks so much!" << endl ;

    return 0 ;
    }


    so as you can see it strips commas and puts a space in its place.

    Thanks for you help :-)

  • Thomas Matthews

    #2
    Re: strip commas from string

    ineedyourluvin1 @yahoo.com wrote:[color=blue]
    > Hello all! I've been looking for a way to strip characters from strings
    > such as a comma. This would be great for using a comma
    > as a delimiter. I show you what I have right now.[/color]

    Thank you for posting code. This really helps.
    [color=blue]
    >
    >
    > #include<iostre am>
    > #include<string >[/color]
    You're not using std::string. So you _may_ not need
    this header.

    [color=blue]
    >
    > int main(int argc, char *argv[])
    > {
    > using namespace std ;
    >
    > char people[14] = "Me,Myself, I" ;
    > char nocommas[14] ;
    > int i, j ;
    > i=j=0 ;
    >
    >
    >
    >
    >
    > for(i = 0; i < 14; i++){
    > if(people[i] != ',')
    > nocommas[i] = people[i] ;
    >
    > if(people[i] == ',')
    > nocommas[i] = ' ' ;
    > }
    >
    >
    >
    > cout << "With commas : " << people << endl ;
    > cout << "Without commas : " << nocommas << endl ;
    >
    > cout << "\n\n\n\n" ;
    > cout << "\tNow tell me how I can store each name in a separate
    > variable ?" << endl ;
    > cout << "\tAnd can someone show me how to do this with string instead
    > of char[] ?" << endl ;
    > cout << "\t\t Thanks so much!" << endl ;
    >
    > return 0 ;
    > }
    >
    >
    > so as you can see it strips commas and puts a space in its place.
    >
    > Thanks for you help :-)
    >[/color]

    1. See std::string class, especially the constructor:
    std::string(cha r *)
    So that you can use the find() member of the string class.

    2. Also explore the strtok() and strchr() functions in the
    "C" standard library.

    --
    Thomas Matthews

    C++ newsgroup welcome message:

    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.l earn.c-c++ faq:

    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library

    Comment

    • Alipha

      #3
      Re: strip commas from string

      > Hello all! I've been looking for a way to strip characters from strings[color=blue]
      > such as a comma. This would be great for using a comma
      > as a delimiter. I show you what I have right now.[/color]
      [color=blue]
      > cout << "\n\n\n\n" ;
      > cout << "\tNow tell me how I can store each name in a separate
      > variable ?" << endl ;
      > cout << "\tAnd can someone show me how to do this with string instead
      > of char[] ?" << endl ;
      > cout << "\t\t Thanks so much!" << endl ;
      >
      > return 0 ;
      > }
      >[/color]

      to store "each name in a separate variable" you'll want a std::vector,
      another container, or perhaps an array.

      to split the string based upon commas, you can put the string in a
      std::stringstre am and then use std::getline:

      std::istringstr eam ss("I,Myself,Me ");
      std::string name;
      while(std::getl ine(ss, name, ',') {
      /* push_back name onto the end of a std::vector or another container
      */
      }

      Comment

      • Larry I Smith

        #4
        Re: strip commas from string

        ineedyourluvin1 @yahoo.com wrote:[color=blue]
        > Hello all! I've been looking for a way to strip characters from strings
        > such as a comma. This would be great for using a comma
        > as a delimiter. I show you what I have right now.
        >
        >
        > #include<iostre am>
        > #include<string >
        >
        > int main(int argc, char *argv[])
        > {
        > using namespace std ;
        >
        > char people[14] = "Me,Myself, I" ;
        > char nocommas[14] ;
        > int i, j ;
        > i=j=0 ;
        >
        >
        >
        >
        >
        > for(i = 0; i < 14; i++){
        > if(people[i] != ',')
        > nocommas[i] = people[i] ;
        >
        > if(people[i] == ',')
        > nocommas[i] = ' ' ;
        > }
        >
        >
        >
        > cout << "With commas : " << people << endl ;
        > cout << "Without commas : " << nocommas << endl ;
        >
        > cout << "\n\n\n\n" ;
        > cout << "\tNow tell me how I can store each name in a separate
        > variable ?" << endl ;
        > cout << "\tAnd can someone show me how to do this with string instead
        > of char[] ?" << endl ;
        > cout << "\t\t Thanks so much!" << endl ;
        >
        > return 0 ;
        > }
        >
        >
        > so as you can see it strips commas and puts a space in its place.
        >
        > Thanks for you help :-)
        >[/color]

        Here's one possibility:


        #include <iostream>
        #include <sstream>
        #include <string>
        #include <list>

        int main(int argc, char *argv[])
        {
        // the test string of comma-seperated words
        char * people = "Me,Myself, I";

        // make an input stream named 'in' from 'people'
        std::istringstr eam in(people);

        // 'words' will hold the list of words extracted from 'in'
        std::list< std::string > words;

        // while no error on 'in'
        while(in)
        {
        // 'aWord' will hold the next word read from 'in'
        std::string aWord;

        // get all text from 'in' up to the next comma, or end of data,
        // into 'aWord'
        getline(in, aWord, ',');

        // if no error on 'in', i.e. we read text into 'aWord',
        // then put a copy of 'aWord' in to the 'words' list
        if (in)
        words.push_back (aWord);
        }

        std::cout << "Comma seperated words read from 'people' into the
        'words' list"
        << std::endl;

        // 'it' is an iterator for iterating over the content of the
        // 'words' list
        std::list< std::string >::iterator it;

        // print all of the entries in 'words'
        for (it = words.begin(); it != words.end(); it++)
        std::cout << (*it) << std::endl;

        return 0;
        }

        Regards,
        Larry

        Comment

        • Sebastian Hungerecker

          #5
          Re: strip commas from string

          ineedyourluvin1 @yahoo.com wrote:[color=blue]
          > Hello all! I've been looking for a way to strip characters from strings
          > such as a comma. This would be great for using a comma
          > as a delimiter. I show you what I have right now.[/color]

          I'm pretty new to C++ myself (I started learning two or three weeks
          ago), so this is probably not the cleanest (or best) way to do this,
          but it works - for me anyway (it compiles with g++ -Wall -ansi -pedantic
          without any warnings, so it should probably work everwhere else, too).

          I stayed pretty close to the code you already had, so you shouldn't have
          any problems understanding it.


          #include <iostream>
          #include <string>
          #include <vector>

          using namespace std;

          int main(int argc, char **argv) {
          string commas="Me,Myse lf,I";
          if(argv[1]) commas=argv[1]; // Use commandline parameter as string
          // if specified.
          vector<string> nocommas(1);
          int j=0;
          for(unsigned int i=0; i<commas.length (); i++) {
          if(commas[i]==',') { // If the current character is a comma,
          nocommas.push_b ack(""); // add a new (empty) string to the vector
          j++; // and increase the counter so that all
          // future operations are performed on
          the
          // new string
          }
          else nocommas.at(j)+ =commas[i]; // If it's not add the current char
          // to the string with the number j
          // in the vector
          }
          cout<<"With commas: "<<commas<<endl <<endl;
          cout<<"From the array:"<<endl;
          for(int i=0; i<=j; i++) {
          cout<<"Word "<<i<<": "<<nocommas.at( i)<<endl;
          }
          return 0;
          }

          --
          If geiger counter does not click,
          the coffee, she is just not thick

          Comment

          • ineedyourluvin1@yahoo.com

            #6
            Re: strip commas from string

            Larry, why use std:: all the time? You can just put 'using namespace
            std' at the beginning of your code :-)

            Comment

            • Larry I Smith

              #7
              Re: strip commas from string

              ineedyourluvin1 @yahoo.com wrote:[color=blue]
              > Larry, why use std:: all the time? You can just put 'using namespace
              > std' at the beginning of your code :-)
              >[/color]

              That can get you in trouble.
              In a small program like this it's ok, but it can cause
              unexpected problems in large apps where name conflicts
              might occur with methods from other (3rd party) libs.

              I explicitly use 'std::' in example programs so the reader
              will be clear on where things come from in the example.

              Regards,
              Larry

              Comment

              • Larry I Smith

                #8
                Re: strip commas from string

                Larry I Smith wrote:[color=blue]
                > ineedyourluvin1 @yahoo.com wrote:[color=green]
                >>Larry, why use std:: all the time? You can just put 'using namespace
                >>std' at the beginning of your code :-)
                >>[/color]
                >
                > That can get you in trouble.
                > In a small program like this it's ok, but it can cause
                > unexpected problems in large apps where name conflicts
                > might occur with methods from other (3rd party) libs.
                >
                > I explicitly use 'std::' in example programs so the reader
                > will be clear on where things come from in the example.
                >
                > Regards,
                > Larry
                >[/color]


                For completeness, this line in my example:

                getline(in, aWord, ',');

                should be:

                std::getline(in , aWord, ',');

                In the absence of a "using namespace std;" declaration,
                some compilers will compile the example ok, but some
                will complain about not being able to find the appropriate
                getline() function.

                Regards,
                Larry

                Comment

                • ineedyourluvin1@yahoo.com

                  #9
                  Re: strip commas from string

                  >That can get you in trouble.[color=blue]
                  >In a small program like this it's ok, but it can cause
                  >unexpected problems in large apps where name conflicts
                  >might occur with methods from other (3rd party) libs.[/color]

                  Ok Larry, I see now. You know I learn way more in the news groups
                  than I do from a text book ! By the way as far as text books go,
                  I'm studying from the Pearson Textbook series , C++ Today and C++
                  Primer.
                  They're good books but have me starting my progs with using namespace
                  and
                  sticking with char[] instead of string alot. And to sebastian, Alipha,
                  and Thomas ,
                  I appreciate your help too ! Larry, I never knew that putting 'Using
                  namespace whatever'
                  would get me into trouble until you demonstrated it to me in this
                  forum. From now
                  on I'm going to append std:: to all my std library functions and
                  objects.
                  I had to say all that to demonstrate my appreciation ... Thanks

                  Comment

                  • Larry I Smith

                    #10
                    Re: strip commas from string

                    ineedyourluvin1 @yahoo.com wrote:[color=blue][color=green]
                    >>That can get you in trouble.
                    >>In a small program like this it's ok, but it can cause
                    >>unexpected problems in large apps where name conflicts
                    >>might occur with methods from other (3rd party) libs.[/color]
                    >
                    > Ok Larry, I see now. You know I learn way more in the news groups
                    > than I do from a text book ! By the way as far as text books go,
                    > I'm studying from the Pearson Textbook series , C++ Today and C++
                    > Primer.
                    > They're good books but have me starting my progs with using namespace
                    > and
                    > sticking with char[] instead of string alot. And to sebastian, Alipha,
                    > and Thomas ,
                    > I appreciate your help too ! Larry, I never knew that putting 'Using
                    > namespace whatever'
                    > would get me into trouble until you demonstrated it to me in this
                    > forum. From now
                    > on I'm going to append std:: to all my std library functions and
                    > objects.
                    > I had to say all that to demonstrate my appreciation ... Thanks
                    >[/color]

                    The use of 'using namespace std;' versus explicit 'std::' is a decision
                    that needs to be made on a project-by-project basis. If you are not
                    linking with multiple libs and the project is small, then the 'using'
                    declaration is fine. If the project is large and you are linking
                    with multiple libs then explicit use of 'std::' might be a better
                    option. Most IT departments have some kind of guidelines for their
                    developers spelling out the corporation's C++ standards. It just
                    happens that my employer expects the use of 'std::' in all but the
                    most trivial programs.

                    Like anything else, do what makes sense for the particular project.

                    Regards,
                    Larry

                    Comment

                    • ineedyourluvin1@yahoo.com

                      #11
                      Re: strip commas from string

                      What do you mean by linking with multiple libs ? You mean like if I
                      want to use
                      ncurses in a Linux program I would link more than one lib by doing :

                      mycomp$> gcc -o ncursesprog ncursesprog.cpp -lncurses

                      By putting the -lncurses I would be linking with more than one lib then
                      ?

                      Comment

                      • Larry I Smith

                        #12
                        Re: strip commas from string

                        ineedyourluvin1 @yahoo.com wrote:[color=blue]
                        > What do you mean by linking with multiple libs ? You mean like if I
                        > want to use
                        > ncurses in a Linux program I would link more than one lib by doing :
                        >
                        > mycomp$> gcc -o ncursesprog ncursesprog.cpp -lncurses
                        >
                        > By putting the -lncurses I would be linking with more than one lib then
                        > ?
                        >[/color]

                        In general, yes -lncurses causes an additional lib to be linked
                        in addition to the Std libs that are linked by default.

                        Always use 'g++' rather than 'gcc' to compile and link C++
                        programs. g++ passes additional info to the linker specific
                        to C++ programs (like the list of Std libs to link, etc). e.g.

                        g++ -o ncursesprog ncursesprog.cpp -lncurses

                        Use 'gcc' for 'C' programs and 'g++' for C++ and mixed
                        'C' & C++ programs.

                        The newsgroup for GCC 'gcc' is: gnu.gcc.help
                        The newsgroup for GCC 'g++' is: gnu.g++.help

                        Regards,
                        Larry

                        Comment

                        • Julián Albo

                          #13
                          Re: strip commas from string

                          Larry I Smith wrote:
                          [color=blue]
                          > The use of 'using namespace std;' versus explicit 'std::' is a decision
                          > that needs to be made on a project-by-project basis. If you are not[/color]

                          What do you mean? Do you think is reasonable in any project to force to use
                          a "using namespace std" in all translation units? Or to put in in header
                          files? If it is that, I don't think that nobody will agree with that.

                          Unless the project is sooooo small that nobody cares about his maintenance,
                          of course.

                          --
                          Salu2

                          Comment

                          • Larry I Smith

                            #14
                            Re: strip commas from string

                            Julián Albo wrote:[color=blue]
                            > Larry I Smith wrote:
                            >[color=green]
                            >>The use of 'using namespace std;' versus explicit 'std::' is a decision
                            >>that needs to be made on a project-by-project basis. If you are not[/color]
                            >
                            > What do you mean? Do you think is reasonable in any project to force to use
                            > a "using namespace std" in all translation units? Or to put in in header
                            > files? If it is that, I don't think that nobody will agree with that.
                            >
                            > Unless the project is sooooo small that nobody cares about his maintenance,
                            > of course.
                            >[/color]

                            No, In fact, we never use 'using namespace std;', but in a very small
                            program, like the example code in this thread, it's ok.

                            Regards,
                            Larry

                            Comment

                            • ineedyourluvin1@yahoo.com

                              #15
                              Re: strip commas from string

                              So what would be the std libs I can link with ? I mean is there a such
                              thing as iostream.lib ? I did a harddrive search and nothing like that
                              was found. I would like to know. By the way Larry, I type in the
                              example
                              program you added to this thread which separated words and used
                              the header files sstream and list. Here's the output I got

                              Comma separated words read from 'people' into the 'words' list
                              me
                              myself
                              i

                              I like the way they list one underneath the other like that . In any
                              case I'm
                              going to need practice using headers list and sstream. So much to
                              learn!

                              Sincerelly,

                              Me

                              Comment

                              Working...