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

5
  • strstr(initialurl, ":") returns the position of colon after http: Commented Aug 18, 2018 at 5:36
  • for this purpose it's better to use regex methods, like regex_search , en.cppreference.com/w/cpp/regex/regex_search Commented Aug 18, 2018 at 5:45
  • Your algorithm is very poor. Lets imagine http://example.demo.com/demo/set=:xxx Commented Aug 18, 2018 at 5:46
  • Please explain what is memory management about this. Commented Aug 18, 2018 at 5:59
  • you're better off using a library for this, as recommended in the linked question (just ignore the accepted answer). Your specific code has several issues, not least of which is that you're modifying string literals. Commented Aug 18, 2018 at 6:02

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.