Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to match the first word after an expression in a string with C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void find_next_word(const char *text, const char *expression) {
    char *pos = strstr(text, expression);
    if (pos) {
        pos += strlen(expression); // Move past the found expression
        
        // Skip any spaces
        while (*pos && isspace(*pos)) {
            pos++;
        }

        // Extract the next word
        if (*pos) {
            char word[32] = {0}; // Buffer for the word
            int i = 0;
            while (*pos && !isspace(*pos) && i < sizeof(word) - 1) {
                word[i++] = *pos++;
            }
            word[i] = '\0';
            printf("The first word after '%s' is: %s\n", expression, word);
        } else {
            printf("No word found after '%s'.\n", expression);
        }
    } else {
        printf("No match found!\n");
    }
}

int main() {
    const char *text = "The quick brown fox jumps over the lazy dog.";
    const char *expression = "fox";

    find_next_word(text, expression);
    
    return 0;
}



/*
run:

The first word after 'fox' is: jumps

*/

 



answered Jun 14, 2025 by avibootz
...