Eamorr
Eamorr

Reputation: 10012

C beginner: split a string

I have the following string: "http://www.google.ie/".I want to create a string "www.google.ie"

How do I do this in C? Here's what I've tried so far:

char* url="http://www.google.ie/";
char* url_stripped=NULL;
char* out=strtok(url,"http://");
while(out){
    out=strtok(0,".");
    url_stripped=out;
    break;
}
printf("%s\n",url_stripped);

But it's not working.I also fear that if I have a url containing 'h', 't', 't' or 'p', that things will get messed up.

I also need to be able to stip off "https://" from the beginning.

Upvotes: 1

Views: 385

Answers (7)

user379888
user379888

Reputation:

char* url="http://www.google.ie/";
char* url_stripped;
strcpy(url_stripped,url+7);
printf("%s\n",url_stripped);

Upvotes: 1

Gil
Gil

Reputation: 3334

I have the following string: "http://www.google.ie/" I want to create a string "www.google.ie"

You can do this (less code, maximum speed):

#define protocol  "http://"
#define host      "www.google.ie"
#define slash     "/"

// "http://www.google.ie/"
printf("Whole string: %s\n", protocol host slash);

// "www.google.ie"
printf("URL only: %s\n", host);

Easy, right?

Upvotes: 1

hmjd
hmjd

Reputation: 121961

A late possible solution:

const char* PROTOCOLS[]  = { "http://", "https://", 0 };
char* url_stripped = 0;
const char* protocol;
char* url = *(a_argv + 1);

for (size_t i = 0; 0 != PROTOCOLS[i]; i++)
{
    protocol = strstr(url, PROTOCOLS[i]);
    if (protocol == url) /* Ensure starts with and not elsewhere. */
    {
        const char* first_fwd_slash;
        protocol += strlen(PROTOCOLS[i]);

        first_fwd_slash = strchr(protocol, '/');
        if (0 == first_fwd_slash)
        {
            url_stripped = strdup(protocol);
        }
        else
        {
            const size_t size = first_fwd_slash - protocol + 1;
            url_stripped = malloc(sizeof(char) * size);
            memcpy(url_stripped, protocol, size - 1);
            *(url_stripped + size - 1) = 0;
        }
        break;
    }
    url_stripped = 0;
}

if (0 != url_stripped)
{
    printf("[%s]\n", url_stripped);
    free(url_stripped);
}

Upvotes: 1

Matic Oblak
Matic Oblak

Reputation: 16774

There are quite a lot of ways to do that actually. You did not really specify much on what the code should do in general. As in, what do you want to isolate in this string: "http://stackoverflow.com/questions/8387669/c-beginner-split-a-string/"

Anyway, if you just want to lose that "http://" and the last "/", I would suggest using this code:

char url[] = "http://www.google.ie/";
    char url_stripped[100];
    sscanf(url, "http://%s", url_stripped);//get new string without the prefix "http://"
    url_stripped[strlen(url_stripped)-1] = '\0';//delete last charactar (replace with null terminator)
    printf("%s\n",url_stripped);

The "sscanf" function can get very handy in such situations. It works much like "fscanf" and "scanf", but the input is the string. As for the line "char url_stripped[100];" make sure you have enough space or use malloc(strlen(url)+1); and free(); when you don't need the string anymore.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409136

How about checking if the string starts with "http://" or "https://", and then skip seven or eight characters, then then search for the first '/'?

char *url="http://www.google.ie/";
char *tmp = url;
char *stripped_url;

if (strncmp(tmp, "http://", 7) == 0 || strncmp(tmp, "https://", 8) == 0)
    tmp += (tmp[4] == 's') ? 8 : 7;  /* Skip over the "http://" or "https://" */

char *slash = strchr(tmp, '/');
if (slash != NULL)
    stripped_url = strndup(tmp, slash - tmp);  /* slash-tmp is the length between start of the string and the slash */
else
    stripped_url = strdup(tmp);

printf("domain name = \"%s\"\n", strupped_url);

free(stripped_url);

Upvotes: 1

andrea.marangoni
andrea.marangoni

Reputation: 1499

C library gives you a lot of function to work with! So, a suggestion to begin with, is to give a look here: http://www.cplusplus.com/reference/clibrary/cstring/ so you can choose the function that fits your needs.. instead of reinventing the algorithm i suggest to work with you already have! good work!

Upvotes: 3

Mansuro
Mansuro

Reputation: 4617

You should tokenize using the /

char url[]="http://www.google.ie/";
char* url_stripped=strtok(url,"/");
url_stripped=strtok(NULL,"/");
printf("%s\n",url_stripped);

Upvotes: 1

Related Questions