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.
Sun qingyao
source share