Words into a vector

This is a discussion on Words into a vector within the C++ Programming forums, part of the General Programming Boards category; Hi. I was wondering if anyone would give me a hint or an idea on how to take a string ...

  1. #1
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51

    Words into a vector

    Hi. I was wondering if anyone would give me a hint or an idea on how to take a string and put each word in the string into the vector. How would I go about doing this?

  2. #2

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    You could use strtok() within a loop to find each token in the c-string delimited by a space, and push it into the vector.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    That solution may or may not be easy, but definitely not a good approach for C++.
    Use std::string instead, along with find and substr.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    I was using std::string. The question in the excercise was:

    "Read some text into a vector, storing each word in the input as an element in the vector. transform each word into uppercase letters. Print the transformed elements from the vector, printing eight words to a line."

    I done it, but I think it might be a little messy.. lol.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    int main()
    {
    	std::vector<std::string> vText; //Container for each word
    	std::string sText; //String of words
    
    	getline(std::cin, sText); //Get input
    
    	int newPos = 0;
    	int wordCount = 0;
    	std::string temp;
    
    	while(sText.find(' ', newPos) <= sText.size())
    	{
    		wordCount++;
    		newPos = sText.find(' ', newPos) + 1;
    	}
    	newPos = 0;
    	wordCount++;
    
    	for(int i = 0; i < wordCount; i++)
    	{
    		int j = sText.find(' ', newPos);
    		if(newPos > 0)
    		{
    			temp = sText.substr(newPos, j - newPos);
    		}
    		else
    		{
    			temp = sText.substr(newPos, j);
    		}
    		std::transform(temp.begin(), temp.end(), temp.begin(), toupper);
    		vText.push_back(temp);
    
    		newPos = j + 1;
    	}
    	wordCount = 0;
    
    	for(std::vector<std::string>::size_type i = 0; i < vText.size(); i++) //Output each word 8 at a time
    	{
    		if(wordCount <= 8)
    		{
    			std::cout << vText[i] << " ";
    			wordCount++;
    
    			
    		}
    		else
    		{
    			std::cout << "\n" << vText[i] << " ";
    			wordCount = 0;
    		}
    	}
    	std::cout << std::endl;
    
    	return 0;
    }
    Be gentle... very gentle.

    EDIT: Forgot to put them in upper case... done
    Last edited by gin; 06-29-2008 at 01:32 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Use a stream to separate the words, since operator>> stops at whitespace by default. You can either do this when reading from cin, or you can take the line you read in and put it into a stringstream and do it in there.

    >> sText.find(' ', newPos) <= sText.size()
    If you were to keep this code you wouldn't want <= there, since find should never return size and will return the unsigned equivalent of -1 if it fails. I'd use while (sText.find(' ', newPos) != std::string::npos) or even while ((newPos = sText.find(' ', newPos)) != std::string::npos). However, those might not be accurate word counts if there are back to back spaces (using the stream method avoids that problem).
    Last edited by Daved; 06-29-2008 at 02:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 07:00 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

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