Thread: extracting words from an array of words

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    extracting words from an array of words

    Hello all, I'm having a lot of trouble with extracting individual words from an array of words. First I input a file into an array, and then I'm trying to extract each word into a seperate cstring, I can do it if the number of words is given, but if not then I'm drawing a blank. But, lets say that I know that maximum number of words is 25, what then?

    this is what I have so far:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    const int SIZE = 81;
    const int MAX_WORDS = 25;
    
    int main()
    {
    
    	ifstream inStream;
    	//ofstream outStream;
    	
    	inStream.open("darwin.txt");
    	if ( inStream.fail() ) {
          cout << "Input file opening for darwin.txt failed.  Exiting...\n\n";
          exit(-1);
        }
     
    	
    	char theWords[MAX_WORDS][SIZE];
    	int wordRow = 0;
    	int count = 0;         
                    int i;
    	int numberOfWords=0;
    	char j; 
    	char *pWord;
    	char word[SIZE/2][SIZE];
    	int c=0;
    	
    	while ( inStream >> theWords[wordRow]) 
    	{
          wordRow++;
        }
        ............and here I'm drawing a blank
    
    	
    	return 0;
    should I test if the an element of theWords array is a blank space " ", but then how could I account for punctuation? I know I don't have much done this time, but any suggestions would be much appreciated,

    thanks

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    if your array of chars is null terminated i.e. a c style string then you could use strtok(). many examples on the boards. search button is up and right!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Thanks a lot, I did used search before posting but I was using different keywords...it's weird that my book doesn't even include the strtok() in its contexts...this makes things much simpler...I'll keep working on it...thanks a lot,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting words from strings
    By umerkk in forum C Programming
    Replies: 4
    Last Post: 04-15-2009, 09:13 AM
  2. Array splicing and extracting
    By INFERNO2K in forum C Programming
    Replies: 5
    Last Post: 08-04-2007, 05:38 PM
  3. Begginer Problem: Extracting words from sentence
    By barlas in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2006, 03:17 PM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM