Thread: Help with a vector of strings!

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    Help with a vector of strings!

    I need to write a C++ program taking a vector of strings and separating out the words in each, storing them into a vector, but I don't know how. Could anyone help me please?

    Code:
    Template Code:
    
    vector <string> seperateWords (vector<string> lines) {
        vector<string > words;
        
        //Separating stuff goes here
    
        return words;
    }
    Thanks in advance!

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Code:
    top: 
      Read in a line 
      position to start of line 
      wordfind: 
        find next non-blank - remember address #1 
        find next blank or end of string - whichever comes first - remember address #2 
        address #2 - address #1 = length of word 
        Using address #1 and length, add the string to your vector
      goto wordfind. 
    goto top.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    Thanks. Could you please explain what you've done there?

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I wrote some pseudo code and enclosed it in [code] and [/code] tags.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Of course, in actual C++, you will find it a lot more comfortable to just use std::stringstream.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by CornedBee View Post
    Of course, in actual C++, you will find it a lot more comfortable to just use std::stringstream.
    and operator>> to read from it part of the string upto next whitespace
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08: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. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM