Extract web addresses from string in C

I have problems with my code and I need your help! I need to write a function that will retrieve a web address starting with www. and ending with .edu from the entered string. The string you enter will not contain spaces, so scanf() should work well here.

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

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

 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 should use pointer arithmetic. The errors I get have something to do with incompatible types and casting. Thanks in advance!

+7
c pointers parsing
source share
3 answers

Thus, the most trivial approach could be:

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

Now here is 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); } 

Please note: s2 must be large enough to contain the extracted web address, or you can get segfault.

+1
source share

This is a simple solution for your problem:

 char* extract(char *s1) { char* ptr_www; char* ptr_edu; int len ; char* s2; ptr_www = strstr(s1,"www"); ptr_edu = strstr(s1,".edu"); len = ptr_edu -ptr_www + 4; s2 = malloc (sizeof(char)*len+1); strncpy(s2,ptr_www,len); s2[len] = '\0'; printf ("%s",s2); return s2; } 
0
source share

Unfortunately, there are many mistakes. Your compilation fails because you pass char to strcat when it expects char *. Even if it compiled, although it would crash.

 for(p = s1; p < (s1 + size); p++) { // This if statement will reference beyond s1+size when p=s1+size-2. Consequently it may segfault if(*p=='w' && *(p+1)=='w' && *(p+2)=='w' && *(p+3)=='.') { for(p; p < (p+4); p++) // This is an infinite loop // strcat concatenates one string onto another. // Dereferencing the pointer makes no sense. // This is the likely causing your compilation error. // If this compiled it would almost certainly segfault. strcat(*j, *p); } // This will also reference beyond s1+size. Consequently it may segfault else if(*p=='.' && *(p+1)=='e' && *(p+2)=='d' && *(p+3)=='u') { for(p; (p+1) < (p+4); p++) // This is also an infinite loop // Again strcat expects 2x char* (aka. strings) not 2x char // This will also almost certainly segfault. strcat(*j, *p); } } // strlen() counts the number of chars until the first '\0' occurrence // It is never correct to call strlen() to determine where to add a '\0' string termination character. // If the character were actually absent this would almost certainly result in a segfault. // As it is strcat() (when called correctly) will add the terminator anyway. size = strlen(j); *(j+size+1) = '\0'; 

EDIT: This seems like a homework question, so I thought it would be more constructive to mention where your current code is going wrong, so you can double-check your knowledge in these areas.

The answer to your exact question is that it does not compile because you are casting a string and therefore passing 2x char instead of char * to strcat ().

-one
source share

All Articles