Home > Net >  Extract number after subtring in c
Extract number after subtring in c

Time:10-11

I am trying to extract numbers from an HTML input field in C. Given the following HTTP post request, how can I extract the numbers 12 and 29 after the num1 and num2 substring and multiply them?

POST /test HTTP/1.1
Host: foo.example
Content-Type: application/x-www-form-urlencoded
Content-Length: 27

num1=12&num2=29

CodePudding user response:

If the string still has the same form, you can use sscanf. Ex:

#include <stdio.h>

int main (void)
{
    char *str = "num1=12&num2=29";

    int num1, num2;

    if(sscanf(str, "num1=%d&num2=%d", &num1, &num2)==2)
    {
        printf("num1 = %d\n", num1);
        printf("num2 = %d\n", num2);
    }
    else puts("Error");

    return 0;
}
  • Related