You are wrong. The function *actually* checks to see if the string is easily incorporated into a rap and emails Matthew Sobol's daemon. (along with other background tasks...) :PA perfectly valid assumption for a function named StringIsUpper
You are wrong. The function *actually* checks to see if the string is easily incorporated into a rap and emails Matthew Sobol's daemon. (along with other background tasks...) :PA perfectly valid assumption for a function named StringIsUpper
Fact - Beethoven wrote his first symphony in C
Spent some time last reworking the code with some suggestions.
I decided to just split on the space and check each token to see if it matches with the all words.
Seems like this works exactly how I need it.
Code:#include <regex.h> #include <stdio.h> #include <stdlib.h> #include <string.h> enum { FALSE, TRUE }; typedef struct { char* name; char words[500]; } Person; int main() { int start; int count; char* re; char* token; char* regex; char words[100][500]; char* speakers[100]; regmatch_t m[2]; regex_t reg; size_t length; re = " "; regex = "^ *[A-Z]+ *: *(.*)"; char text[1000] = "BOB: Hello there, my friend. ROBERT: How are you, friend? Have you heard about Justin's new game? SPEAKER: I don't know what you're talking about, homie. BOB: Yes, you know what I mean. BOB: I also know what I'm talking about."; token = strtok(text, re); start = TRUE; count = 0; while (token != NULL) { if (regcomp(®, regex, REG_EXTENDED) != 0) { perror("regcomp"); exit(EXIT_FAILURE); } if (regexec(®, token, 2, m, 0) == 0) { // remove colon length = strlen(token); token[length - 1] = '\0'; if (start == TRUE) { start = FALSE; speakers[count] = token; token = strtok(NULL, re); } else { count++; speakers[count] = token; token = strtok(NULL, re); } } strcat(words[count], token); strcat(words[count], " "); token = strtok(NULL, re); } Person people[count]; Person p; for (int i = 0; i < count; i++) { p.name = speakers[i]; strcpy(p.words, words[i]); people[i] = p; } for (int i = 0; i < count; i++) { printf("{%s | %s}\n", people[i].name, people[i].words); } }
Could be easier if you use ':' and punctuation ('.!?') as delimiters to strtok. No need to regex.
This is just an example, but the real text I'm using contains colons. Here is an example:
"BOB: I love the weather, but there is a problem: I hate going outdoors."
Well, that's not necessarily a problem. What you haven't done is precisely define the input format. Regex is one way to do that, but of course any reasonable human-readable way to express the grammar could work.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You could use English, as long as you were sufficiently detailed.
My point is that when you want to parse something, you need to be absolutely clear as to its format/grammar. Unless you literally mean that you only have that specific string in mind, saying "I'm trying to split this string" is not good. People will make suggestions based on what they think are the salient points of the example, but they could be wrong. So, save them the effort. Don't say in post #19 that "This is just an example, but the real text I'm using contains colons.". From the outset; state what the format is so people know that embedded colons are possible. Spaces between words? Multiple spaces that you want to retain? Whitespace other than spaces? Alphanumeric? How do we know to identify SPEAKER, SPEAKER2? Like, are they really SPEAKER followed by a colon or a number then a colon? Or could it be BOB?
Last edited by laserlight; 06-20-2019 at 10:23 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)