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 6570011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:42:11+00:00 2026-05-25T14:42:11+00:00

I need to parse input from a user that could be any number of

  • 0

I need to parse input from a user that could be any number of variations:
1+1 4( 3-0 ) =x 1*(3)-8

How do I do this using scanf to get the raw_input, then split out all of the different values and tell either if it is a string ie x = – () or a int?

This is what I was thinking

    char * raw_input;
    scanf("%s",raw_input);

It takes a array of char and then I just need to split and convert into a single elements. What is the best way of doing the input and (splitting and converting)

Thanks

  • 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-05-25T14:42:11+00:00Added an answer on May 25, 2026 at 2:42 pm

    If you want to write your own code, the best way is to define your expression in form of a grammar. To easily parse the grammar, it’s best to make it simple.

    For example, to parse expressions in the form like this (1+(3*4+x)*y)+1, you could write such a grammar:

    Expression -> Addition | null
    Addition -> Multiplication RestOfAddition
    RestOfAddition -> null | + Addition
    Multiplication -> Element RestOfMultiplication
    RestOfMultiplication -> null | * Element
    Element -> number | variable | ( Expression )
    

    Then in your program, for every non-terminal in this grammar (the ones on the left of ->), you write one function, like this:

    ExpTree *Expression(char *exp, int *position)
    {
        if (exp[*position])
        {
            ExpTree *node = malloc(sizeof(*node));
            node->type = LAMBDA;
            node->value = 0;
            return node;
        }
        else
            return Addition(exp, position);
    }
    
    ExpTree *Addition(char *exp, int *position)
    {
        ExpTree *node = malloc(sizeof(*node));
        node->type = ADDITION;
        node->left = Multiplication(exp, position);
        node->right = RestOfAddition(exp, position);
        return node;
    }
    
    ExpTree *RestOfAddition(char *exp, int *position)
    {
        ExpTree *node;
        if (exp[*position] == '+')
        {
            ++*position;
            return Addition(exp, position);
        }
        else
        {
            ExpTree *node = malloc(sizeof(*node));
            node->type = LAMBDA;
            node->value = 0;
            return node;
        }
    }
    

    Similarly, Multiplication and RestOfMultiplication would be written as functions.

    ExpTree *Element(char *exp, int *position)
    {
        if (exp[*position] == '(')
        {
            ExpTree *node;
            ++*position;
            node = Expression(exp, position);
            if (!exp[*position] != ')')
                 printf("Expected ) at position %d\n", *position);
            else
                 ++*position;
            return node;
        }
        else if (exp[*position] == ')')
        {
            printf("Unexpected ) at position %d\n", *position);
            return NULL;
        }
        else if (exp[*position] >= '0' && exp[*position] <= '9')
        {
            ExpTree *node = malloc(sizeof(*node));
            node->type = INTEGER;
            node->value = extract_int(exp, position);
            return node;
        }
        else if ((exp[*position] >= 'a' && exp[*position] <= 'z') ||
                 (exp[*position] >= 'A' && exp[*position] <= 'Z') ||
                 exp[*position] == '_')
        {
            ExpTree *node = malloc(sizeof(*node));
            node->type = VARIABLE;
            node->value = extract_variable(exp, position);
            return node;
        }
        else
        {
            printf("Warning: unexpected character %c in location %d\n", exp[*position], *position);
            return NULL;
        }
    }
    

    Where extract_int and extract_variable are two functions that take the expression and the position on it, go ahead while they are seeing a number (or a letter in the extract_variable function) they build the number (variable) from the expression string and return it, setting position to after where they finished.

    Note: This is not code for copy paste. It is not complete and lacks sufficient error checking. Some details have been omitted and is offered as a solution to teach how simple parsing is done rather than easiest solution.

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

Sidebar

Related Questions

I need to parse a line from cmd that looks like this SOME WHITE
I have a rather big number of source files that I need parse and
Need to parse a file for lines of data that start with this pattern
I'm writing a very simple program where I want to get user input from
I need the Perl regex to parse plain text input and convert all links
I'm working on an application using OpenGL and C++ that parses some structured input
I have a website where I need to parse date/time strings from receipts. These
When a user is inputting something, I need to check user input against the
I'm trying to get a line as input from the command line. My problem
I've got a fairly big XML file that I need to parse into a

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.