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

  • SEARCH
  • Home
  • 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 7803555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:37:15+00:00 2026-06-02T01:37:15+00:00

I want to parse user input commands in my C (just C) program. Sample

  • 0

I want to parse user input commands in my C (just C) program. Sample commands:

add node ID

add arc ID from ID to ID

print

exit

and so on. Then I want to do some validation with IDs and forward them to specified functions. Functions and validations are of course ready. It’s all about parsing and matching functions…

I’ve made it with many ifs and strtoks, but I’m sure it’s not the best way… Any ideas (libs)?

  • 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-02T01:37:16+00:00Added an answer on June 2, 2026 at 1:37 am

    I think what you want is something like this:

    while (1) 
    {
        char *line = malloc(128); // we need to be able to increase the pointer
        char *origLine = line;
        fgets(line, 128, stdin);
    
        char command[20];
        sscanf(line, "%20s ", command);
    
        line = strchr(line, ' ');
    
        printf("The Command is: %s\n", command);
    
        unsigned argumentsCount = 0;
        char **arguments = malloc(sizeof(char *));
    
        while (1)
        {
            char arg[20];
            if (line && (sscanf(++line, "%20s", arg) == 1))
            {
                arguments[argumentsCount] = malloc(sizeof(char) * 20);
                strncpy(arguments[argumentsCount], arg, 20);
    
                argumentsCount++;
    
                arguments = realloc(arguments, sizeof(char *) * argumentsCount + 1);
                line = strchr(line, ' ');
            }
            else {
                break;
            }
        }
    
        for (int i = 0; i < argumentsCount; i++) {
            printf("Argument %i is: %s\n", i, arguments[i]);
        }
    
        for (int i = 0; i < argumentsCount; i++) {
            free(arguments[i]);
        }
    
        free(arguments);
        free(origLine);
    }  
    

    You can do what you wish with ‘command’ and ‘arguments’ just before you free it all.

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

Sidebar

Related Questions

I'm writing a very simple program where I want to get user input from
I want to parse the input string and extract the values from it. My
I want to parse user input using named captures for readability. When they type
I'm trying to parse a URI from user input. I'm assuming some users won't
I'm currently trying to use PhP to parse user input. The page is just
I want to parse, process and then overwrite only 1 specific column in a
I want to parse the tabular information from a .pdf file,and want to display
Original I want to parse a string of html code and add newlines after
What i want to do is each time user clicks the button Add and
In my program I am taking user input and parsing it into a 2d

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.