Counting Strings

This is a discussion on Counting Strings within the C++ Programming forums, part of the General Programming Boards category; How can I count how many strings? For example, Input is: How are you 3 strings if input is: Fine ...

  1. #1
    Unregistered
    Guest

    Counting Strings

    How can I count how many strings?

    For example, Input is:
    How are you

    3 strings

    if input is:
    Fine

    1 string

    How can I do it? What can I use? Thanks!!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    read a whole line (using cin.getline say), then count the spaces.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Unregistered
    Guest
    Thank you!! I learn by myself, no textbook, no material, nothing!! So, would u mind giving me some hints that how to use cin.getline or give me some tutorial web address which talks about cin.getline?! Thank you very much!!

  4. #4
    Unregistered
    Guest
    First I suggest you look up J. Liberty's book Teach Yourself C++ in 21 days. Not everyone likes it, but it is available online for free at several sites, and some people like it a lot.

    the prototype for get line is

    streamName.getline(char *, int, char = '\n');

    meaning getline() is a member function of the istream class. Since ifstream class (used to read files) is derived from istream, getline() is also a member function of ifstream class.

    getline() takes three parameters. The first is a char array to hold the string read in. The second is the maximum number of characters to be read in by getline() if the terminating char isn't found before the maximum number of char is read. The third parameter is the terminating char. If this character is encountered before the maximum number of characters is read in it will terminate input. The third parameter defaults to new line char, and is frequently left out of function calls for that reason.

    getline() removes the terminating char from the input stream it is reading, unlike other istream member funcitons like >> and get().

    getline() treats white space characters like space, tab, newline, etc. just like any other character unlike >>.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > Thank you!! I learn by myself, no textbook, no material, nothing!!
    You're not going to get far like that

    This should keep you off the streets for a while
    http://directory.google.com/Top/Comp...and_Tutorials/

    http://www.mindview.net/Books
    Thinking in C++ seems a good bet as well.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Unregistered
    Guest
    Thank you very much!!

  7. #7
    Unregistered
    Guest
    I get stuck! Can anyone post some example codes?? Not code for me, but teach me by telling examples... pls... thx!!~

  8. #8
    Unregistered
    Guest
    here's a site that has Liberty's book on it. Go to chapter 16 section 7. He has a good example on the use of getline(). Beyond that post code that can be evaluated and assistance provided if appropriate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM
  5. Counting a character with strings
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-06-2002, 08:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21