2

I am having trouble with my C programming and I would like some help in extracting a set of characters from a character array. The following below is in a character array:

POST / HTTP/1.1
Host: localhost:8081
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: 11
Origin: http://localhost:8081
Connection: keep-alive
Referer: http://localhost:8081/
Upgrade-Insecure-Requests: 1
textbox=asd

I would like to extract "asd" from this entire chunk, how do I do this? Also, do note that the characters "asd" can be changed to any character or length. Please help..

4
  • Do you want the content copied, or simply point to the start of the string after textbox=? Commented Apr 6, 2021 at 4:07
  • i need the content after "textbox=" to be copied Commented Apr 6, 2021 at 4:09
  • Formatting note: you can force newlines by putting two space characters at the end of each line. But I find it easier just to convert the text into a code block. Commented Apr 6, 2021 at 4:53
  • ahh, i see!! thank you! Commented Apr 6, 2021 at 4:57

1 Answer 1

2

You can use strstr to search for textbox= and copy the rest of the string with strdup:

char *p = strstr(str, "textbox=");
if (p)
    char *t = strdup(p+sizeof("textbox=")-1);
Sign up to request clarification or add additional context in comments.

2 Comments

thank you so much for your help, it worked!
Note that strdup() uses malloc() to create the returned string, that has to be passed to free() once you finish with it, or the memory will stay allocated and your program could run out of available memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.