How do I parse a string into individual characters? (really simple!) really!

Discussion in 'C++' started by Jeannie, Aug 23, 2005.

  1. Jeannie

    Jeannie Guest

    Hello group!

    I'm in Europe, traveling with my laptop, and I don't any compilers
    other than Borland C++ 5.5. available. I also don't have any manuals
    or help files available. Sadly, more crippling problems! I never used
    C++ much, so, I don't remember commands or fuctions well. My questions
    are simple. I posted the DOS window output so you can see the Borland
    C++ version I am using:

    E:\TZ>bcc32 file2
    Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
    File2.cpp:
    Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

    What are the commands or functions needed to parse a string into it's
    individual characters?

    Example:

    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <iostream.h>
    int main(void)
    { char MyString[80] ;
    printf("Input a string: ");
    gets(MyString);
    printf("The string input was: %s\n", MyString);
    return 0;
    }

    If I have a string input from the keyboard as: Good Morning!
    I want a variable, maybe a vector, such as
    vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!

    I will need to determine the length of the string in bytes, and then
    move through the string to the end. How do I declare a character
    vector variable called "vec" here, and determine the length? Then I
    can loop through until the end.

    Thanks a million!

    Jeannie
    Jeannie, Aug 23, 2005
    #1
    1. Advertising

  2. Jeannie

    benben Guest

    "Jeannie" <> wrote in message
    news:...
    > Hello group!
    >
    > I'm in Europe, traveling with my laptop, and I don't any compilers
    > other than Borland C++ 5.5. available. I also don't have any manuals
    > or help files available. Sadly, more crippling problems! I never used
    > C++ much, so, I don't remember commands or fuctions well. My questions
    > are simple. I posted the DOS window output so you can see the Borland
    > C++ version I am using:
    >
    > E:\TZ>bcc32 file2
    > Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
    > File2.cpp:
    > Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
    >
    > What are the commands or functions needed to parse a string into it's
    > individual characters?
    >
    > Example:
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    > #include <time.h>
    > #include <iostream.h>


    Should be <iostream>

    > int main(void)
    > { char MyString[80] ;
    > printf("Input a string: ");
    > gets(MyString);
    > printf("The string input was: %s\n", MyString);
    > return 0;
    > }
    >
    > If I have a string input from the keyboard as: Good Morning!
    > I want a variable, maybe a vector, such as
    > vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!


    In C++, arrays are zero-based, you start with vec[0].

    >
    > I will need to determine the length of the string in bytes, and then
    > move through the string to the end. How do I declare a character
    > vector variable called "vec" here, and determine the length? Then I
    > can loop through until the end.
    >
    > Thanks a million!
    >
    > Jeannie


    Whatever, there's the standard library to do all these for you:

    #include <iostream>
    #include <string>

    using namespace std;

    string s;
    getline(cin, s);

    // now if you input "Good" then
    // s[0]='G', s[1]='o', s[2]='o', s[3]='d'

    Ben
    benben, Aug 23, 2005
    #2
    1. Advertising

  3. Jeannie

    Default User Guest

    Re: How do I parse a string into individual characters? (really simple!) really!

    Jeannie wrote:

    > Hello group!
    >
    > I'm in Europe, traveling with my laptop, and I don't any compilers
    > other than Borland C++ 5.5. available. I also don't have any manuals
    > or help files available. Sadly, more crippling problems! I never used
    > C++ much, so, I don't remember commands or fuctions well. My questions
    > are simple. I posted the DOS window output so you can see the Borland
    > C++ version I am using:
    >
    > E:\TZ>bcc32 file2
    > Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
    > File2.cpp:
    > Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
    >
    > What are the commands or functions needed to parse a string into it's
    > individual characters?
    >
    > Example:
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    > #include <time.h>
    > #include <iostream.h>
    > int main(void)
    > { char MyString[80] ;
    > printf("Input a string: ");
    > gets(MyString);
    > printf("The string input was: %s\n", MyString);
    > return 0;
    > }
    >
    > If I have a string input from the keyboard as: Good Morning!
    > I want a variable, maybe a vector, such as
    > vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!
    >
    > I will need to determine the length of the string in bytes, and then
    > move through the string to the end. How do I declare a character
    > vector variable called "vec" here, and determine the length? Then I
    > can loop through until the end.



    That's not a good way to do what you want. What exactly do you want to
    do with this vector besides find the length of the string?

    My suggestion would be:

    #include <string>
    #include <iostream>

    // less typing for example
    using namespace std;

    int main()
    {
    string MyString;

    getline(cin, MyString);

    cout << "Length = " << MyString.length() << '\n';

    for (int i = 0; i < MyString.length(); i++)
    cout << "MyString[" << i << "] " << MyString << '\n';

    return 0;
    }

    hello

    Length = 5
    MyString[0] h
    MyString[1] e
    MyString[2] l
    MyString[3] l
    MyString[4] o




    Brian
    Default User, Aug 23, 2005
    #3
  4. Jeannie

    Jeannie Guest

    On Tue, 23 Aug 2005 20:24:26 +1000, "benben" <moc.liamtoh@hgnohneb
    read backward> wrote:

    >
    >"Jeannie" <> wrote in message
    >news:...
    >> Hello group!
    >>
    >> I'm in Europe, traveling with my laptop, and I don't any compilers
    >> other than Borland C++ 5.5. available. I also don't have any manuals
    >> or help files available. Sadly, more crippling problems! I never used
    >> C++ much, so, I don't remember commands or fuctions well. My questions
    >> are simple. I posted the DOS window output so you can see the Borland
    >> C++ version I am using:
    >>
    >> E:\TZ>bcc32 file2
    >> Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
    >> File2.cpp:
    >> Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
    >>
    >> What are the commands or functions needed to parse a string into it's
    >> individual characters?
    >>
    >> Example:
    >>
    >> #include <stdlib.h>
    >> #include <stdio.h>
    >> #include <time.h>
    >> #include <iostream.h>

    >
    >Should be <iostream>
    >
    >> int main(void)
    >> { char MyString[80] ;
    >> printf("Input a string: ");
    >> gets(MyString);
    >> printf("The string input was: %s\n", MyString);
    >> return 0;
    >> }
    >>
    >> If I have a string input from the keyboard as: Good Morning!
    >> I want a variable, maybe a vector, such as
    >> vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!

    >
    >In C++, arrays are zero-based, you start with vec[0].
    >
    >>
    >> I will need to determine the length of the string in bytes, and then
    >> move through the string to the end. How do I declare a character
    >> vector variable called "vec" here, and determine the length? Then I
    >> can loop through until the end.
    >>
    >> Thanks a million!
    >>
    >> Jeannie

    >
    >Whatever, there's the standard library to do all these for you:
    >
    > #include <iostream>
    > #include <string>
    >
    > using namespace std;
    >
    > string s;
    > getline(cin, s);
    >
    > // now if you input "Good" then
    > // s[0]='G', s[1]='o', s[2]='o', s[3]='d'
    >
    >Ben
    >



    Ben, this looks great, thanks!
    Jeannie, Aug 23, 2005
    #4
  5. Jeannie

    Jeannie Guest

    Re: How do I parse a string into individual characters? (really simple!) really!

    On 23 Aug 2005 17:56:37 GMT, "Default User" <>
    wrote:

    >Jeannie wrote:
    >
    >> Hello group!
    >>
    >> I'm in Europe, traveling with my laptop, and I don't any compilers
    >> other than Borland C++ 5.5. available. I also don't have any manuals
    >> or help files available. Sadly, more crippling problems! I never used
    >> C++ much, so, I don't remember commands or fuctions well. My questions
    >> are simple. I posted the DOS window output so you can see the Borland
    >> C++ version I am using:
    >>
    >> E:\TZ>bcc32 file2
    >> Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
    >> File2.cpp:
    >> Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
    >>
    >> What are the commands or functions needed to parse a string into it's
    >> individual characters?
    >>
    >> Example:
    >>
    >> #include <stdlib.h>
    >> #include <stdio.h>
    >> #include <time.h>
    >> #include <iostream.h>
    >> int main(void)
    >> { char MyString[80] ;
    >> printf("Input a string: ");
    >> gets(MyString);
    >> printf("The string input was: %s\n", MyString);
    >> return 0;
    >> }
    >>
    >> If I have a string input from the keyboard as: Good Morning!
    >> I want a variable, maybe a vector, such as
    >> vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!
    >>
    >> I will need to determine the length of the string in bytes, and then
    >> move through the string to the end. How do I declare a character
    >> vector variable called "vec" here, and determine the length? Then I
    >> can loop through until the end.

    >
    >
    >That's not a good way to do what you want. What exactly do you want to
    >do with this vector besides find the length of the string?
    >
    >My suggestion would be:
    >
    >#include <string>
    >#include <iostream>
    >
    >// less typing for example
    >using namespace std;
    >
    >int main()
    >{
    > string MyString;
    >
    > getline(cin, MyString);
    >
    > cout << "Length = " << MyString.length() << '\n';
    >
    > for (int i = 0; i < MyString.length(); i++)
    > cout << "MyString[" << i << "] " << MyString << '\n';
    >
    > return 0;
    >}
    >
    >hello
    >
    >Length = 5
    >MyString[0] h
    >MyString[1] e
    >MyString[2] l
    >MyString[3] l
    >MyString[4] o
    >
    >
    >Brian


    Brian, this looks great, thanks!
    Jeannie, Aug 23, 2005
    #5
  6. Re: How do I parse a string into individual characters? (really simple!) really!

    Jeannie wrote:
    > Hello group!
    >
    > I'm in Europe, traveling with my laptop, and I don't any compilers
    > other than Borland C++ 5.5. available. I also don't have any manuals
    > or help files available. Sadly, more crippling problems! I never used
    > C++ much, so, I don't remember commands or fuctions well. My questions
    > are simple. I posted the DOS window output so you can see the Borland
    > C++ version I am using:
    >
    > E:\TZ>bcc32 file2
    > Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
    > File2.cpp:
    > Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
    >
    > What are the commands or functions needed to parse a string into it's
    > individual characters?
    >
    > Example:
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    > #include <time.h>
    > #include <iostream.h>
    > int main(void)
    > { char MyString[80] ;
    > printf("Input a string: ");
    > gets(MyString);
    > printf("The string input was: %s\n", MyString);
    > return 0;
    > }
    >
    > If I have a string input from the keyboard as: Good Morning!
    > I want a variable, maybe a vector, such as
    > vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!
    >
    > I will need to determine the length of the string in bytes, and then
    > move through the string to the end. How do I declare a character
    > vector variable called "vec" here, and determine the length? Then I
    > can loop through until the end.
    >
    > Thanks a million!


    Jeannie,

    Since you are using C++ (and already have some answers),
    I'll give you an alternative that makes use of the modern
    standard library features:

    # include <iostream>
    # include <ostream>
    # include <string>
    # include <vector>
    # include <algorithm>

    int main()
    {
    std::string Buffer;

    std::cout << "Please enter a string: ";
    std::getline( std::cin, Buffer );

    // copies each character of 'Buffer' into the vector
    std::vector<char> V( Buffer.begin(), Buffer.end() );

    // Display each character stored in the vector
    std::copy( Buffer.begin(), Buffer.end(),
    std::eek:stream_iterator<char>( std::cout, "\n" ) );

    return 0;
    }

    The Standard C++ Library has a rich set of tools to help you
    achieve your goals in the shortest possible time, so it is a
    good to start getting familiar with them :)

    HTH.

    Cheers,
    Chris Val
    Chris ( Val ), Aug 24, 2005
    #6
  7. Re: How do I parse a string into individual characters? (really simple!) really!

    On Tue, 23 Aug 2005 23:09:40 +0300, Jeannie <> wrote:

    snip 80+ lines
    >Brian, this looks great, thanks!


    Would you please trim your posts. There is no need to quote
    everything just to say thanks.


    <<Remove the del for email>>
    Barry Schwarz, Aug 24, 2005
    #7
  8. Jeannie

    Jeannie Guest

    Re: How do I parse a string into individual characters? (really simple!) really!

    On 23 Aug 2005 20:43:02 -0700, "Chris ( Val )"
    <> wrote:

    ># include <iostream>
    ># include <ostream>
    ># include <string>
    ># include <vector>
    ># include <algorithm>
    >
    >int main()
    > {
    > std::string Buffer;
    >
    > std::cout << "Please enter a string: ";
    > std::getline( std::cin, Buffer );
    >
    > // copies each character of 'Buffer' into the vector
    > std::vector<char> V( Buffer.begin(), Buffer.end() );
    >
    > // Display each character stored in the vector
    > std::copy( Buffer.begin(), Buffer.end(),
    > std::eek:stream_iterator<char>( std::cout, "\n" ) );
    >
    > return 0;
    > }



    Thank you, I'll give this a try as well as the other suggestions.

    J
    Jeannie, Aug 24, 2005
    #8
  9. Jeannie

    Default User Guest

    Re: How do I parse a string into individual characters? (really simple!) really!

    Chris ( Val ) wrote:


    > > I will need to determine the length of the string in bytes, and then
    > > move through the string to the end. How do I declare a character
    > > vector variable called "vec" here, and determine the length? Then I
    > > can loop through until the end.
    > >
    > > Thanks a million!

    >
    > Jeannie,
    >
    > Since you are using C++ (and already have some answers),
    > I'll give you an alternative that makes use of the modern
    > standard library features:


    She already had one of those.

    > # include <iostream>
    > # include <ostream>
    > # include <string>
    > # include <vector>
    > # include <algorithm>
    >
    > int main()
    > {
    > std::string Buffer;
    >
    > std::cout << "Please enter a string: ";
    > std::getline( std::cin, Buffer );
    >
    > // copies each character of 'Buffer' into the vector
    > std::vector<char> V( Buffer.begin(), Buffer.end() );
    >
    > // Display each character stored in the vector
    > std::copy( Buffer.begin(), Buffer.end(),
    > std::eek:stream_iterator<char>( std::cout, "\n" ) );
    >
    > return 0;
    > }



    What advantages does a vector of char have over the string you already
    created? Yes, I know she mentioned a vector in her problem
    decscription, but then she really was looking for design help.

    Reading into a string, then converting to a vector is in my mind a
    complete waste of time, unless some overriding reason that has not been
    specified is present.

    Her requirements were to be able to read in character data into a
    container, get the size, and loop through to the end. As my example
    showed, std::string already has all that.




    Brian
    Default User, Aug 24, 2005
    #9
  10. Jeannie

    benben Guest

    Re: How do I parse a string into individual characters? (really simple!) really!

    > What advantages does a vector of char have over the string you already
    > created? Yes, I know she mentioned a vector in her problem
    > decscription, but then she really was looking for design help.
    >
    > Reading into a string, then converting to a vector is in my mind a
    > complete waste of time, unless some overriding reason that has not been
    > specified is present.


    It depends. A vector guarantees continueous memory block, but a string
    doesn't.

    >
    > Her requirements were to be able to read in character data into a
    > container, get the size, and loop through to the end. As my example
    > showed, std::string already has all that.
    >
    >
    >
    >
    > Brian
    benben, Aug 25, 2005
    #10
  11. Jeannie

    Alex Vinokur Guest

    "Jeannie" <> wrote in message news:...
    [snip]
    > If I have a string input from the keyboard as: Good Morning!
    > I want a variable, maybe a vector, such as
    > vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!
    >
    > I will need to determine the length of the string in bytes, and then
    > move through the string to the end. How do I declare a character
    > vector variable called "vec" here, and determine the length? Then I
    > can loop through until the end.

    [snip]

    See "Simple C/C++ Perfometer: Splitting string into vector of vectors":

    http://groups.google.com/group/perfo/msg/9d49a1be3a5c6335
    http://groups.google.com/group/perfo/msg/f3c775cf7e3cdcf0


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Aug 25, 2005
    #11
  12. Jeannie

    Guest

    Re: How do I parse a string into individual characters? (really simple!) really!

    Chris ( Val ) wrote:

    (snip)

    > # include <iostream>
    > # include <ostream>
    > # include <string>
    > # include <vector>
    > # include <algorithm>


    #include <iterator>

    (snip)

    > // Display each character stored in the vector
    > std::copy( Buffer.begin(), Buffer.end(),
    > std::eek:stream_iterator<char>( std::cout, "\n" ) );
    >


    Just one point to add to this. The ostream_iterator is available
    in the "iterator" header file.

    For more information,

    http://www.sgi.com/tech/stl/ostream_iterator.html

    Rgds,
    anna
    , Aug 25, 2005
    #12
  13. Re: How do I parse a string into individual characters? (really simple!) really!

    Default User wrote:
    > Chris ( Val ) wrote:
    >
    >
    > > > I will need to determine the length of the string in bytes, and then
    > > > move through the string to the end. How do I declare a character
    > > > vector variable called "vec" here, and determine the length? Then I
    > > > can loop through until the end.
    > > >
    > > > Thanks a million!

    > >
    > > Jeannie,
    > >
    > > Since you are using C++ (and already have some answers),
    > > I'll give you an alternative that makes use of the modern
    > > standard library features:

    >
    > She already had one of those.
    >
    > > # include <iostream>
    > > # include <ostream>
    > > # include <string>
    > > # include <vector>
    > > # include <algorithm>
    > >
    > > int main()
    > > {
    > > std::string Buffer;
    > >
    > > std::cout << "Please enter a string: ";
    > > std::getline( std::cin, Buffer );
    > >
    > > // copies each character of 'Buffer' into the vector
    > > std::vector<char> V( Buffer.begin(), Buffer.end() );
    > >
    > > // Display each character stored in the vector
    > > std::copy( Buffer.begin(), Buffer.end(),
    > > std::eek:stream_iterator<char>( std::cout, "\n" ) );
    > >
    > > return 0;
    > > }

    >
    >
    > What advantages does a vector of char have over the string you already
    > created?


    Well, one poster has already mentioned one possible advantage,
    but note that I did not post my code example to demonstrate a
    comparison between std::string and std::vector.

    > Yes, I know she mentioned a vector in her problem
    > decscription, but then she really was looking for design help.


    Well then you have (sort of), answered your own question :)

    What aspect of what I posted do you feel is so wrong that it
    warrants questioning?

    > Reading into a string, then converting to a vector is in my mind a
    > complete waste of time, unless some overriding reason that has not been
    > specified is present.


    Whether it is a complete waste of time or not is not up to me
    to decide really - I just provided what the OP was looking for
    with a modern approach.

    > Her requirements were to be able to read in character data into a
    > container


    True, and the container wanted was a vector<char>().

    QUOTE:
    "How do I declare a character vector variable called "vec" here"

    , get the size, and loop through to the end. As my example
    > showed, std::string already has all that.


    And the vector<char> (what the OP wanted) also has all of that.

    Like I said, I just provided what the OP was looking for with
    a modern approach, and in particular I wanted to demonstrate
    how to initialise the vector with the given sequence of
    characters from a std::string - Note that there is no need
    for looping to do that.

    I appreciate your comments, though I don't really see the need
    for them :) Did you perhaps missunderstand what I was trying to
    demonstrate?

    I'm not always the best of writers (especially when in a hurry),
    that's for sure <G>.

    Cheers,
    Chris Val

    > Brian
    Chris ( Val ), Aug 25, 2005
    #13
  14. Re: How do I parse a string into individual characters? (really simple!) really!

    wrote:
    > Chris ( Val ) wrote:
    >
    > (snip)
    >
    > > # include <iostream>
    > > # include <ostream>
    > > # include <string>
    > > # include <vector>
    > > # include <algorithm>

    >
    > #include <iterator>
    >
    > (snip)
    >
    > > // Display each character stored in the vector
    > > std::copy( Buffer.begin(), Buffer.end(),
    > > std::eek:stream_iterator<char>( std::cout, "\n" ) );
    > >

    >
    > Just one point to add to this. The ostream_iterator is available
    > in the "iterator" header file.


    Indeed it is.

    My compiler (Broland C++ Builder 5.0) lets me get away
    with things like this, so I often forget to include it
    when I'm in a hurry.

    Thank you for the correction :)

    Cheers,
    Chris Val
    Chris ( Val ), Aug 25, 2005
    #14
  15. Re: How do I parse a string into individual characters? (really simple!) really!

    Jeannie wrote:
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    > #include <time.h>
    > #include <iostream.h>
    > int main(void)
    > { char MyString[80] ;
    > printf("Input a string: ");
    > gets(MyString);
    > printf("The string input was: %s\n", MyString);
    > return 0;
    > }
    >


    While many have given you C++ solutions for what you sought, I have one
    comment on your C-ish effort. Forget 'gets'. Use 'fgets' instead.

    - Anand
    Anand Hariharan, Aug 27, 2005
    #15
  16. Jeannie

    Jeannie Guest

    On Tue, 23 Aug 2005 13:04:50 +0300, Jeannie <> wrote:

    >Hello group!


    >-- deleted to thwart net Nazi attacks --


    >Jeannie


    I was able to complete my task with the early suggestions. Thank you
    all for being so helpful!

    J
    Jeannie, Aug 30, 2005
    #16
    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. stef
    Replies:
    6
    Views:
    919
  2. derrick
    Replies:
    5
    Views:
    780
    derrick
    Dec 17, 2004
  3. Replies:
    19
    Views:
    1,127
    Daniel Vallstrom
    Mar 15, 2005
  4. Thomas Kowalski

    Parse string to double really fast

    Thomas Kowalski, Jun 6, 2007, in forum: C++
    Replies:
    17
    Views:
    1,076
    Thomas Kowalski
    Jun 12, 2007
  5. m4dc4p
    Replies:
    6
    Views:
    157
    Christer Nilsson
    Jan 6, 2006
Loading...

Share This Page