Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GetWords function

Status
Not open for further replies.

idinkin

Programmer
Sep 12, 2002
29
IL
Hi all!
I want to create a function that will recieve a sentence (string) as an argument and return an array of words. Could you tell me please how can I do this? (The part of splitting the sentence to words is already ready but I have a problem with returning an array of words).
Thank you!
 
I'm guessing your problem is that you don't know the size of the array needed (correct me if I'm wrong). If this is the case, have a look at the CArray class. This lets you create an Array object which gives you dynamic control over the array size.

Hope this helps

CMR
 
OK. But I don't want to use MFC library.
Can you tell me the solution of the problem without MFC?
 
How about STL then ?

vector<string> is an obvious choice

/JOlesen
 

Maybe you dont have to return an array of words,instead you can pass them
as an argument of your function.

#define _MAXSTRING_ 100

void splitFunction( char* string, char word[][_MAXSTRING_] )
{
....
...
strcpy( word1,word[0] );
strcpy( word2,word[1] );
....
..
}

I think that you should get the idea.
 
sorry i meant:

#define _MAXSTRING_ 100

void splitFunction( char* string, char word[][_MAXSTRING_] )
{
....
...
strcpy( word[0], word1 ); // the changes are here
strcpy( word[1], word2 );
....
..
}
 
or this :
create a CList-Object, create a pointer to that object
pass the pointer to your function and insert the word into
the list using the pointer.
 
2Leibnitz:
Can the argument of the function be passed with out the size of the array.
I mean something like that:
void splitFunction( char* string, char word[][])
{
...
}
 

yes you can write something like:
Code:
void splitFunction( char* string, char **word )
{
     ...
}

also check the code below,it might help you a little bit.

#include <iostream.h>
#include <string.h>
#include <stdlib.h>

#define MAXROW  4
#define MAXCOL  5

void function( char **word )
{
	for( int i = 0; i < MAXROW; i++ )
    {
        strcpy( word[i], &quot;hello&quot; );
		cout<<word[i]<<endl;
    }
}


void main()
{
	char **array;
    
    array = new char*[MAXROW];

	if( array == NULL )
	{
		cerr<<&quot;memory allocation has fail.&quot;<<endl;
		exit(1);
	}

    for( int j = 0; j < MAXROW; j++ ) 
	{
       array[j] = new char[MAXCOL];

	   if( array[j] == NULL )
	   {
		  cerr<<&quot;memory allocation has fail.&quot;<<endl;
		  exit(1);
	   }
	}

	function( array );

	delete [] array;
}
 
just a little correction &quot;MAXCOL&quot; should be a little bigger
at list &quot;6&quot; or higher.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top