So I'm using Mysql to make a text-to-speech engine. Basically any given phrase by the database is returned to my script, which has to say every word and number, if any, by way of voice . For the voices I'm using Asterisk, but that's another problem of my own lol

The problem I'm having is this: the sql results are given in way of arrays, like this:
char test[10][20]

so test[0] is the first line of query, test[1] second and so on.

So I just use the strtok function on the array using " " (space) as the tokenizer and get the words. But strtok uses pointers as the words it stores, right?
How would I go about converting the word the pointer stores into an array? This would be helpful specially if there are any numbers, since I've already programed a function that reads an array of numbers and says it to Asterisk.

while ((row = mysql_fetch_row(res)) != NULL)
		/*Copy the array to an auxiliary array for processing*/
		int i;
		char num;

		/* Convert all letters to lowecase */
		for (i=0 ;test[0][i]; i++)
			test[0][i]= tolower(test[0][i]);
		/* Separate the phrase into words */
		char token[] = " ";
		char *word = NULL;
		word = strtok(test[0], token);
		/* If word is a number */		
		while (word != NULL) 
			if (*word == '0' || *word == '1' || *word == '2' || *word == '3' || *word== '4' || *word == '5' || 
					*word == '6' || *word == '7' || *word == '8' || *word == '9')
				*word=num;  //Here is where I'm just messing around trying to get the contents of word into an array, since my TTS function reads character by character 
			StreamFile(word); //If word is not number, then say it
			word = strtok(NULL,token);

So how would I go about getting the content of word into an array of any kind so I can access individual characters?

I'm kinda noob also so be gentle

Recommended Answers

All 4 Replies

It may help if you gave us an example of what the resultant query/data set looked like and while your at it, could you show us the definition of test.

The result query would be something like:
"Your ticket number is 230 and is being handled by one of our assistants."

Just a char string with letters and numbers. I'm trying to separate the words from the numbers in a primitive way I know.
test is defined as:

char test[20][35];

Try something like below:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

/*our dummy data set*/

#define DATASIZE 100
#define DATSET "ThiS iS ThE mESSaGE 23h to sENd ov4er th4re\n"

int main(int argc, char**argv)
	int i = 0;
	char ch[DATASIZE];
	char *pstr = NULL;

	strncpy(ch, DATSET, DATASIZE);
	for (i = 0; i < strlen(ch); ++i)
		ch[i] = tolower(ch[i]);

	pstr = strtok(ch, " ");
	fprintf(stdout, "%s\n", pstr);
		pstr = strtok('\0', " ");
		if (pstr) fprintf(stdout, "%s\n", pstr);
	while (pstr);


I greatly simplified what you posted...I hope this will do..It parses the data set but doesn't check for numbers.

This is an idiom to avoid (*):

for (i = 0; i < strlen(ch); ++i)

This is more confusing than it ought to be:

pstr = strtok('\0', " ");

You mean NULL. Use NULL.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.