I have a function (in C) that is passed a URL. This function only works if the URL parameter is in the form of www.example.com, not when it is http://www.example.com.
I would like to modify this function so that, when passed a URL in the form of http://www.example.com, it strips the leading http:// (if present), so that it will operate correctly.
How would I do this in C?
Here’s the function in question (let me know if there is a better way to do this):
char* get_ip (char* url)
{
struct hostent* h;
if ((h = gethostbyname(url)) == NULL)
return NULL;
return inet_ntoa(*((struct in_addr*)h->h_addr));
}
There’s always the simple approach:
Or, if you want to get rid of protocol specifiers of that form in general (e.g., https):