c - Parse string and subtract a substring -
i'm parsing http header in c , need subtract host name full url.
i managed full url (http://www.example.com/hello.html) , path name (hello.html) couldn't subtract (full url - path name) host name (example.com).
example full url: http://www.example.com/hello.html - done host name: example.com - todo path name: /hello.html - done
any appreciated. thanks
you can use memcpy
, this:
char *url = "http://www.example.com/hello.html"; // find last index of `/` char *path = url + strlen(url); while (path != url && *path != '/') { path--; } // calculate length of host name int hostlen = path-url; // allocate byte null terminator char *hostname = malloc(hostlen+1); // copy string newly allocated buffer memcpy(hostname, url, hostlen); // null-terminate copied string hostname[hostlen] = '\0'; ... // don't forget free malloc-ed memory free(hostname);
here demo on ideone.
Comments
Post a Comment