C语言 提取网站域名

来源:互联网 发布:u盘启动安装ubuntu系统 编辑:程序博客网 时间:2024/04/29 07:28
#include <stdio.h>#include <string.h>char *getDomain(const char *s){    char *temp = (char *)malloc(strlen(s) + 1);    temp[0] = '\0';    int i, j;    for (i = 0; s[i]; ++i)    {        if (s[i] == ':' && s[i + 1] == '/' && s[i + 2] == '/')        {            for (i = i + 3, j = 0; s[i] > 32 && s[i] < 127 && s[i] != '/';                 ++i, ++j)            {                temp[j] = s[i];                if (s[i] == '@')                {                    j = -1;                }            }            temp[j] = '\0';            while (--j)            {                if (temp[j] == ':')                {                    temp[j] = '\0';                }            }        }    }    char *domain = (char *)malloc(strlen(temp) + 1);    strcpy(domain, temp);    free(temp);    return domain;}int main(int argc, char *argv[]){    const char *a =        "https://username:password@subdomain.mydomain.com:80/dir/page.html";    const char *point = argc > 1 ? argv[1] : a;    char *result = getDomain(point);    puts(result);    free(result);    return 0;}
0 0