0

I have char *initialurl = "http://example.demo.com:80/demo/path"

expecting output:

char *protocol = "https://"
char *url = "example.demo.com"
char *port = "80"
char *path =  "/demo/path"

I tried

char *initialurl = "http://example.demo.com:80/demo/path"
char *port = strstr(initialurl, ":");
if(port) {
     port = strtok(port, "/") + 1;
     printf("PORT: %s\n", port);
}

Similarly, I am trying to use same char *initialurl to split. By then, memory value of variable changing to "http://example.demo.com".

How can i get path?? Do I need to copy char *initialurl to new variable?

Please suggest. Thank you

  • strstr(initialurl, ":") returns the position of colon after http: – Ali Aug 18 '18 at 05:36
  • for this purpose it's better to use regex methods, like regex_search , https://en.cppreference.com/w/cpp/regex/regex_search – Ali Aug 18 '18 at 05:45
  • Your algorithm is very poor. Lets imagine `http://example.demo.com/demo/set=:xxx` – 0___________ Aug 18 '18 at 05:46
  • Please explain what is memory management about this. – Yunnosch Aug 18 '18 at 05:59
  • you're better off using a library for this, as recommended in the [linked question](https://stackoverflow.com/questions/726122/best-ways-of-parsing-a-url-using-c) (just ignore the accepted answer). Your specific code has several issues, not least of which is that you're [modifying string literals](https://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-string-initialized-with-cha). – Sander De Dycker Aug 18 '18 at 06:02

0 Answers0