Skip to main content
gamedev.net
🔒 Locked

An array of strings

Started by Dest 2 replies 1k views
Original Post
Dest
Dest
How would I make an array of strings? I want to take a sentance and split it up into words and store them in an array. This is probably pretty simple but I ran into problems when I was trying to do it. Any help would be much appreciated.
ziplux
ziplux
You could do something like this:
  
typedef struct sentence
{
char text[100];
};
...
sentence sentencearray[10];

// or...


char sentences[10][100]; // not sure if this creates more elements than necessary



Using a struct or class approach, you could make a function that dynamically allocated the memory for the sentence if you need more than 100 or if the sentences are much different in length.

I''ve also heard of something called STL which has a string class. I don''t know anything about that though...




My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t X
R- tv+ b++ DI+(+++) D- G e* h!"
Decode my geekcode!
Geekcode.com


Visit our web site:
Asylum Entertainment
BriarLoDeran
BriarLoDeran

#include < string >
#include < vector >
using namespace std;

int main(void)
{
vector< string > sentence;

// then you could set the size for the number of words

sentence.resize(10); //or whatever...

// then say ...

sentence[0] = "HAPPY";
sentence[1] = "FISH";
sentence[2] = "ARE";
sentence[3] = "YUMMY.";
// etc, etc...


// got it?
}


By the way, this IS using the STL that ziplux mentioned...

happy coding...?!?

Briar->LoDeran();

Edited by - BriarLoDeran on December 5, 2000 8:35:18 PM
Stoffel
Stoffel
Here''d be an STL way to do it (although if you don''t know about arrays, it''s probably over your head):
  
#include <string>
#include <vector>
#include <ctype.h> // for isspace


// move parts of the std namespace into file namespace

using std::string;
using std::vector;

// convenient typedef

typedef vector<string> StringVec;

// str is the null-terminated input sentence.

// vec is the output array of strings.

int makeSentenceIntoStringArray (const char* str, StringVec& vec)
{
vec.clear (); // destroy output vector


while (true) // going to rely on a break statement

{
// move str to the first non-whitespace character

while (*str && isspace (*str))
str++;

if (*str == 0)
break; // end of string


// find end of this word

char* pEnd = str+1;
while (*pEnd && !isspace (*pEnd))
pEnd++;

// add string to end of vector.

// string is created using the constructor that takes a

// char pointer to the beginning of the string and the length

// of the string as parameters

vec.push_back (string (str, pEnd - str));

// if we hit the end of the sentence, break.

// otherwise, set the loop to look at one past the end of

// this word for the next word

if (*pEnd == 0)
break;
str = pEnd+1;
}
return vec.size (); // = # of words in sentence

}


There you have it. There are some issues using a vector in this way, namely that the vector will have to recopy itself every time it overgrows its bounds. It''s really more of a list of word--you could substitute "list" for "vector" and everything would work. However, if you''re going to USE this container to find, say, the odd words in the sentence or word #5, then you want a vector. A change might be to read it in to a local list in the function, then copy the list into a vector and return it.

Hope it''s not way over your head.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.