Parse string and subtract a substring

I'm parsing a http header in C and need to subtract host name from the full url.

I managed to get the full url ( and the path name (hello.html) but couldn't subtract (full url - path name) host name (

Example full url: - DONE
host name: - TODO
path name: /hello.html - DONE

Any help would be appreciated. Thanks


You can use memcpy, like this:

char *url = "";
// find the last index of `/`
char *path = url + strlen(url);
while (path != url && *path != '/') {
// Calculate the length of the host name
int hostLen = path-url;
// Allocate an extra byte for null terminator
char *hostName = malloc(hostLen+1);
// Copy the string into the newly allocated buffer
memcpy(hostName, url, hostLen);
// Null-terminate the copied string
hostName[hostLen] = '\0';
// Don't forget to free malloc-ed memory

Here is a demo on ideone.

Need Your Help

Upgrading Pyramid/SQLAlchemy web apps

python sqlalchemy pyramid paster sqlalchemy-migrate

I've got a standard run of the mill Pylons Pyramid application, that uses SQLAlchemy for its database persistence.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.