Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8701891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:30:46+00:00 2026-06-13T02:30:46+00:00

I am trying to make a small test function that when passed a char

  • 0

I am trying to make a small test function that when passed a char * will search that string for a certain sub string and then output the next characters after the white space until the next whitespace.

I made a basic implementation using strstr() and strncpy() but this method is kind of static and only works for one search term at a time with a fixed output of the next characters.

int ParseCommand(char *command)
{
    char *pSearch = strstr(command, CommandOne);
    char str[100];

    if (pSearch != NULL)
    {
        pSearch += strlen(CommandOne) + 1;
        strncpy(str, pSearch, 2);
        printf("%s\n\n", str);
    }

    printf("%s\n", command);
    return 0;
}

What this code sample does is if you pass say ParseCommand("ten hats 10 are cool") and CommandOne is equal to "hats" the function will output "10". Although this does work it performs the operations too statically and will make it hard for it to search for more commands within char *command. I essentially need something that will loop through command until strstr() finds a command inside the passed string and then copy everything from after the command to the next white space.

I know how I would search for the commands (I am going to create a pointer char array with all my search terms and loop through them all until strstr() does not return null) but how about would I copy the next “word” after the searched term?

Overall I need some pseudocode logic to search for search terms within a sentence and then copy the data preseeding it until the next whitespace is reached. (Copy the next word after the search term in the sentence.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:30:47+00:00Added an answer on June 13, 2026 at 2:30 am

    I whipped a quick prototype and it seems to work.

    char *ParseCommand(char *command, char *find)
    {
        char *p, *q, *t;
    
        p = strstr(command, find);
    
        if (p != NULL) {
            /* skip to the next word */
            for (q = p; *q != '\0' && *q != ' '; q++)
                ;
            if (*++q != '\0') {
                for (p = q; *q != '\0' && *q != ' '; q++)
                    ;
                t = malloc(q - p);
                return strncpy(t, p, q - p);
            }
        }
    
        return NULL;
    }
    

    Test it with, ParseCommand("ten hats 10 are cool", "hats") and it returns 10.

    HTH.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a factory that will have the type passed in,
im trying to make a small name summary function depending on the size of
I'm trying to make a small Python script that is executed by clicking an
I'm trying to make a small program that could intercept the open process of
I'm trying to make a small VBScript that compacts a MS Access 2007 database
i m trying to make small test driven example by using cppunit. So i
Below is a small test case that demonstrates a problem that I am trying
I'm trying to make a small testing server for Bugzilla so I can test
I am trying to make a small form that lets the user pick one
i am trying to make small 3D scene where are 4 pyramids that can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.