Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting web addresses from a string in C

I have trouble with my code and I need your help! What I need to do is to write a function that will extract the web address that starts from www. and ends with .edu from an inputted string. The inputted string will have no spaces in it so scanf() should work well here.

For example:
http://www.school.edu/admission. The extracted address should be www.school.edu.

This is what I came up with so far, it obviously didn't work, and I can't think of anything else unfortunately.

void extract(char *s1, char *s2) {
    int size = 0;
    char *p, *j;

    p = s1; 
    j = s2;
    size = strlen(s1);

    for(p = s1; p < (s1 + size); p++) {
        if(*p == 'w' && *(p+1) == 'w' && *(p+2) == 'w' && *(p+3) == '.'){
            for(p; p < (p+4); p++)
                strcat(*j, *p);
        }
        else if(*p=='.' && *(p+1)=='e' && *(p+2)=='d' && *(p+3)=='u'){
            for(p; (p+1) < (p+4); p++)
                strcat(*j, *p);                    
        }   
    }
    size = strlen(j);
    *(j+size+1) = '\0';
}

The function has to use pointer arithmetic. The errors I get have something to do with incompatible types and casting. Thanks ahead!

like image 464
Iceberg233 Avatar asked Feb 28 '16 07:02

Iceberg233


1 Answers

So the most trivial approach might be:

#include <stdio.h>

int main(void)
{
    char str[1000];
    sscanf("http://www.school.edu/admission", "%*[^/]%*c%*c%[^/]", str);
    puts(str);
}

Now, here goes the fixed code:

#include <stdio.h>
#include <string.h>

void extract(char *s1, char *s2) {
    size_t size = strlen(s1), i = 0;
    while(memcmp(s1 + i, "www.", 4)){
        i++;
    }
    while(memcmp(s1 + i, ".edu", 4)){
        *s2++ = *(s1 + i);
        i++;
    }
    *s2 = '\0';
    strcat(s2, ".edu");
}

int main(void)
{
    char str1[1000] = "http://www.school.edu/admission", str2[1000];
    extract(str1, str2);
    puts(str2);
}

Note that s2 must be large enough to contain the extracted web address, or you may get a segfault.

like image 72
nalzok Avatar answered Oct 17 '22 00:10

nalzok