DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Re: String Manipulation - strtok and strpbrk

  1. #1
    Christopher Thomas McGinlay Guest

    Re: String Manipulation - strtok and strpbrk

    Coincidentally, I've just reinvented this particular wheel. Never mind.
    Can't find strtok in Stroustrup's book, but found strpbrk() which
    seems to be for C-style strings. Am I right in saying the find()
    function of std::string would be the ANSI way to go on this? If it is
    I'll roll my version of this off the nearest cliff (and there are plenty
    of them round where I live).

    Ta!

    Chris


    ch0rlt0n wrote:

    > Have a look at the strtok function in your C++ help. Pass in "/" as the token
    > string and subsequent calls should return the date, the month and then the
    > year as character buffers. You can then use atoi() to convert the string
    > to an integer and sort...
    >
    > "Stacy" <swagner001@yahoo.com> wrote:
    >
    >> Hey! I am a little bit lost on a project I am working on. Basically I have
    >> to prompt the user for 10 first and last names, and then their birth dates.
    >> Then I use a menu to see what they want this sorted by. I have an idea on
    >> how to do the first and last name sorts, but I have no idea how to manipulate
    >> the birthdate entries to elimate the / when they enter mm/dd/yyyy and then
    >> make it sort by year. If anyone knows I would really appreciate a point

    >
    > in
    >
    >> the right direction! Thanks! Here is the code I have so far.
    >>



    --
    Ascent Software Limited Registered in Scotland: SC201671
    www.ascent.zetnet.co.uk Bank Of Scotland Buildings
    Lerwick, Shetland, ZE1 0EB


  2. #2
    Stacy Guest

    Re: String Manipulation - strtok and strpbrk


    I could not find strtok either. So how will the find() function emliminate
    the / from mm/dd/yyyy? I am still new at C++ so I am not sure how to use
    that to my advantage. Thanks for all your help!

    Christopher Thomas McGinlay <ascent@zetnet.co.uk> wrote:
    >Coincidentally, I've just reinvented this particular wheel. Never mind.


    > Can't find strtok in Stroustrup's book, but found strpbrk() which
    >seems to be for C-style strings. Am I right in saying the find()
    >function of std::string would be the ANSI way to go on this? If it is
    >I'll roll my version of this off the nearest cliff (and there are plenty


    >of them round where I live).
    >
    >Ta!
    >
    >Chris
    >
    >
    >ch0rlt0n wrote:
    >
    >> Have a look at the strtok function in your C++ help. Pass in "/" as the

    token
    >> string and subsequent calls should return the date, the month and then

    the
    >> year as character buffers. You can then use atoi() to convert the string
    >> to an integer and sort...
    >>
    >> "Stacy" <swagner001@yahoo.com> wrote:
    >>
    >>> Hey! I am a little bit lost on a project I am working on. Basically I

    have
    >>> to prompt the user for 10 first and last names, and then their birth

    dates.
    >>> Then I use a menu to see what they want this sorted by. I have an idea

    on
    >>> how to do the first and last name sorts, but I have no idea how to manipulate
    >>> the birthdate entries to elimate the / when they enter mm/dd/yyyy and

    then
    >>> make it sort by year. If anyone knows I would really appreciate a point

    >>
    >> in
    >>
    >>> the right direction! Thanks! Here is the code I have so far.
    >>>

    >
    >
    >--
    >Ascent Software Limited Registered in Scotland: SC201671
    >www.ascent.zetnet.co.uk Bank Of Scotland Buildings
    > Lerwick, Shetland, ZE1 0EB
    >



  3. #3
    Danny Kalev Guest

    Re: String Manipulation - strtok and strpbrk

    strtok is declared in <cstring>

    Danny

    Stacy wrote:
    >
    > I could not find strtok either. So how will the find() function emliminate
    > the / from mm/dd/yyyy? I am still new at C++ so I am not sure how to use
    > that to my advantage. Thanks for all your help!
    >
    > Christopher Thomas McGinlay <ascent@zetnet.co.uk> wrote:
    > >Coincidentally, I've just reinvented this particular wheel. Never mind.

    >
    > > Can't find strtok in Stroustrup's book, but found strpbrk() which
    > >seems to be for C-style strings. Am I right in saying the find()
    > >function of std::string would be the ANSI way to go on this? If it is
    > >I'll roll my version of this off the nearest cliff (and there are plenty

    >
    > >of them round where I live).
    > >
    > >Ta!
    > >
    > >Chris
    > >
    > >
    > >ch0rlt0n wrote:
    > >
    > >> Have a look at the strtok function in your C++ help. Pass in "/" as the

    > token
    > >> string and subsequent calls should return the date, the month and then

    > the
    > >> year as character buffers. You can then use atoi() to convert the string
    > >> to an integer and sort...
    > >>
    > >> "Stacy" <swagner001@yahoo.com> wrote:
    > >>
    > >>> Hey! I am a little bit lost on a project I am working on. Basically I

    > have
    > >>> to prompt the user for 10 first and last names, and then their birth

    > dates.
    > >>> Then I use a menu to see what they want this sorted by. I have an idea

    > on
    > >>> how to do the first and last name sorts, but I have no idea how to manipulate
    > >>> the birthdate entries to elimate the / when they enter mm/dd/yyyy and

    > then
    > >>> make it sort by year. If anyone knows I would really appreciate a point
    > >>
    > >> in
    > >>
    > >>> the right direction! Thanks! Here is the code I have so far.
    > >>>

    > >
    > >
    > >--
    > >Ascent Software Limited Registered in Scotland: SC201671
    > >www.ascent.zetnet.co.uk Bank Of Scotland Buildings
    > > Lerwick, Shetland, ZE1 0EB
    > >


  4. #4
    Christopher Thomas McGinlay Guest

    Re: String Manipulation - strtok and strpbrk

    Is the following any use? I've used std::string instead of char arrays

    #include <string>
    #include <iostream>

    int main(void)
    {
    std::string dob="11/24/01"; //mm/dd/yy

    int erase_pos = dob.find("/", 0); //start at '0'
    while(erase_pos!=dob.npos) //npos is not founc
    {
    dob.erase(erase_pos,1); //erase 1 character
    erase_pos = dob.find("/", 0);
    }

    std::cout << dob << std::endl; //mmddyy
    return 0;
    }

    Cheers,

    Chris

    Stacy wrote:

    > I could not find strtok either. So how will the find() function emliminate
    > the / from mm/dd/yyyy? I am still new at C++ so I am not sure how to use
    > that to my advantage. Thanks for all your help!
    >
    > Christopher Thomas McGinlay <ascent@zetnet.co.uk> wrote:
    >
    >> Coincidentally, I've just reinvented this particular wheel. Never mind.

    >
    >
    >> Can't find strtok in Stroustrup's book, but found strpbrk() which
    >> seems to be for C-style strings. Am I right in saying the find()
    >> function of std::string would be the ANSI way to go on this? If it is
    >> I'll roll my version of this off the nearest cliff (and there are plenty

    >
    >
    >> of them round where I live).
    >>
    >> Ta!
    >>
    >> Chris
    >>
    >>
    >> ch0rlt0n wrote:
    >>
    >>
    >>> Have a look at the strtok function in your C++ help. Pass in "/" as the

    >>

    > token
    >
    >>> string and subsequent calls should return the date, the month and then

    >>

    > the
    >
    >>> year as character buffers. You can then use atoi() to convert the string
    >>> to an integer and sort...
    >>>
    >>> "Stacy" <swagner001@yahoo.com> wrote:
    >>>
    >>>
    >>>> Hey! I am a little bit lost on a project I am working on. Basically I
    >>>

    > have
    >
    >>>> to prompt the user for 10 first and last names, and then their birth
    >>>

    > dates.
    >
    >>>> Then I use a menu to see what they want this sorted by. I have an idea
    >>>

    > on
    >
    >>>> how to do the first and last name sorts, but I have no idea how to manipulate
    >>>> the birthdate entries to elimate the / when they enter mm/dd/yyyy and
    >>>

    > then
    >
    >>>> make it sort by year. If anyone knows I would really appreciate a point
    >>>
    >>> in
    >>>
    >>>
    >>>> the right direction! Thanks! Here is the code I have so far.
    >>>>
    >>>

    >>
    >> --
    >> Ascent Software Limited Registered in Scotland: SC201671
    >> www.ascent.zetnet.co.uk Bank Of Scotland Buildings
    >> Lerwick, Shetland, ZE1 0EB
    >>



    --
    Ascent Software Limited Registered in Scotland: SC201671
    www.ascent.zetnet.co.uk Bank Of Scotland Buildings
    Lerwick, Shetland, ZE1 0EB


  5. #5
    Stacy Guest

    Re: String Manipulation - strtok and strpbrk


    So here is how I tried to use it, I have never used that before, so this is
    all guesswork...

    struct Info
    {
    char fname [20]; //First Name
    char lname [20]; //Last Name
    char bdate [20]; //Unsorted Birthday

    };

    void main(void)
    {

    Info person[2];


    for(int ctr=0; ctr<2; ctr++)
    {
    cout<<"Please enter a name (first then last): ";
    cin>> person[ctr].fname;
    cin>> person[ctr].lname;
    cout<<"Now enter their birth-date (mm/dd/yyyy): ";
    cin>>person[ctr].bdate;

    int erase_pos = person[ctr].bdate.find("/", 0);

    while(erase_pos!= person[ctr].bdate.npos)
    {
    person[ctr].bdate.erase(erase_pos,1);
    erase_pos = person[ctr].bdate.find("/", 0);
    }

    cout << Info.bdate << endl; //mmddyy
    return 0;


    }

    Christopher Thomas McGinlay <ascent@zetnet.co.uk> wrote:
    >Is the following any use? I've used std::string instead of char arrays
    >
    >#include <string>
    >#include <iostream>
    >
    >int main(void)
    >{
    > std::string dob="11/24/01"; //mm/dd/yy
    >
    > int erase_pos = dob.find("/", 0); //start at '0'
    > while(erase_pos!=dob.npos) //npos is not founc
    > {
    > dob.erase(erase_pos,1); //erase 1 character
    > erase_pos = dob.find("/", 0);
    > }
    >
    > std::cout << dob << std::endl; //mmddyy
    > return 0;
    >}
    >
    >Cheers,
    >
    >Chris
    >
    >Stacy wrote:
    >
    >> I could not find strtok either. So how will the find() function emliminate
    >> the / from mm/dd/yyyy? I am still new at C++ so I am not sure how to use
    >> that to my advantage. Thanks for all your help!
    >>
    >> Christopher Thomas McGinlay <ascent@zetnet.co.uk> wrote:
    >>
    >>> Coincidentally, I've just reinvented this particular wheel. Never mind.

    >>
    >>
    >>> Can't find strtok in Stroustrup's book, but found strpbrk() which
    >>> seems to be for C-style strings. Am I right in saying the find()
    >>> function of std::string would be the ANSI way to go on this? If it is


    >>> I'll roll my version of this off the nearest cliff (and there are plenty

    >>
    >>
    >>> of them round where I live).
    >>>
    >>> Ta!
    >>>
    >>> Chris
    >>>
    >>>
    >>> ch0rlt0n wrote:
    >>>
    >>>
    >>>> Have a look at the strtok function in your C++ help. Pass in "/" as

    the
    >>>

    >> token
    >>
    >>>> string and subsequent calls should return the date, the month and then
    >>>

    >> the
    >>
    >>>> year as character buffers. You can then use atoi() to convert the string
    >>>> to an integer and sort...
    >>>>
    >>>> "Stacy" <swagner001@yahoo.com> wrote:
    >>>>
    >>>>
    >>>>> Hey! I am a little bit lost on a project I am working on. Basically

    I
    >>>>

    >> have
    >>
    >>>>> to prompt the user for 10 first and last names, and then their birth
    >>>>

    >> dates.
    >>
    >>>>> Then I use a menu to see what they want this sorted by. I have an idea
    >>>>

    >> on
    >>
    >>>>> how to do the first and last name sorts, but I have no idea how to

    manipulate
    >>>>> the birthdate entries to elimate the / when they enter mm/dd/yyyy and
    >>>>

    >> then
    >>
    >>>>> make it sort by year. If anyone knows I would really appreciate a point
    >>>>
    >>>> in
    >>>>
    >>>>
    >>>>> the right direction! Thanks! Here is the code I have so far.
    >>>>>
    >>>>
    >>>
    >>> --
    >>> Ascent Software Limited Registered in Scotland: SC201671
    >>> www.ascent.zetnet.co.uk Bank Of Scotland Buildings
    >>> Lerwick, Shetland, ZE1 0EB
    >>>

    >
    >
    >--
    >Ascent Software Limited Registered in Scotland: SC201671
    >www.ascent.zetnet.co.uk Bank Of Scotland Buildings
    > Lerwick, Shetland, ZE1 0EB
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center