English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Like if i input: "Hello World", how can i count the words and assign each word in an array(eg: word[0]="Hello", word[1]="World"?

Thanks.

2007-07-31 13:43:37 · 4 answers · asked by Jeffrey J 1 in Computers & Internet Programming & Design

4 answers

#include
#include
/* returns a null terminated array of character pointers
* simular to *argv[]
*/
char **StringToWords(char *InString)
{
char *pnt;
char **rc_array;
int spacecnt = 0;

for (pnt = InString; *pnt; pnt++)
{
if (*pnt == ' ' || *pnt == '\t' || *pnt == '\n' || *pnt == '\r')
spacecount++;
}
rc_array = (char **)calloc(spacecnt + 1, sizeof(char *));
if (rc == (char **)0)
{
perror("calloc() failure");
exit(0);
}
i = 0;
pnt = InString;
while (rc_array[i++] = (char *)strtok(pnt, " \t\r\n"))
pnt = (char *)0;
return rc_array;
}

2007-07-31 13:59:20 · answer #1 · answered by Dave H 4 · 0 0

The easiest way to split it is using the strtok command. You'll need studio.h included:

char * szString = "Hello World";
char * StringArray[80]
var count = 0;
char * tok = strtok(szString, " ");
StringArray[count] = tok;
count++;
while (tok != NULL) {
// Do something with the tok
tok = strtok(NULL," ");
StringArray[count] = tok;
count++;
}

Take the above with a grain of salt, i dont have a C compiler anymore and im a little rusty. But i do hope it sends you in the right direction.

2007-07-31 21:21:05 · answer #2 · answered by madludwigv 2 · 0 0

iterate through your starting string with a loop and parse the whitespace characters (or use something like strtok, strstr, etc) then add the words to your list array.

2007-07-31 20:53:18 · answer #3 · answered by mdigitale 7 · 0 0

you could probally write a short macro in Excel... Not quite sure what your trying to do here... just asign a number to each word????

2007-07-31 20:47:55 · answer #4 · answered by Anonymous · 0 1

fedest.com, questions and answers