2303 - 统一资源定位符

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 32 MB
  统一资源定位符(Uniform Resource Locator,缩写为URL)是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址。互联网上的每个文件都有一个唯一的URL,它包含的信息指出文件的位置以及浏览器应该怎么处理它。
典型的URL语法应该如下:
scheme://domain:port/path?query_string#fragment_id
在这个问题中模式/协议(scheme)和域名(domain)是必须的其他都是可选的。
例如下面包含一些正确的URL:
http://dict.bing.com.cn/#%E5%B0%8F%E6%95%B0%E7%82%B9
http://www.mariowiki.com/Mushroom
https://mail.google.com/mail/?shva=1#inbox
http://en.wikipedia.org/wiki/Bowser_(character)
http://fync.acmclub.com/
ftp://222.207.30.4/
http://www.int255.com:8080/bbs/
现在你的任务是从给定的URL中找出域名。

题目输入

包含多组测试用例,每个URL各占一行,请读到文件末尾。

题目输出

对于每组测试用例,你应该输出给定URL中的域名,每个输出各占一行。

输入/输出样例

输入格式

http://dict.bing.com.cn/#%E5%B0%8F%E6%95%B0%E7%82%B9
http://www.mariowiki.com/Mushroom
https://mail.google.com/mail/?shva=1#inbox

输出格式

dict.bing.com.cn
www.mariowiki.com
mail.google.com

C语言解答

#include<stdio.h>
#include<string.h>
int main()
{
    int i,d;
    char str[10000];
    while(scanf("%s",str)!=EOF)
    {
        d=0;       
        for(i=0;str[i]!='\0';i++)
        {
            if(!d)
            {
              if(str[i]=='/'&&str[i-1]=='/')
                  d=1;
            }
            else 
            {if(str[i]==':'||str[i]=='/')
                        break;
                printf("%c",str[i]);
            }
        }
        printf("\n");
    }
return 0;
}


  

C++解答

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str;
    while(cin>>str)
    {
        int pos1=str.find("//");
        int pos2=str.find(':',pos1+2);
        if(pos2==string::npos)
            pos2=str.find('/',pos1+2);
        cout<<str.substr(pos1+2,pos2-pos1-2)<<endl;
    }
    return 0;
}