码迷,mamicode.com
首页 > Web开发 > 详细

Parsing URL

时间:2015-02-15 21:57:01      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

Parsing URL

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1693    Accepted Submission(s): 864


Problem Description
In computing, a Uniform Resource Locator or Universal Resource Locator (URL) is a character string that specifies where a known resource is available on the Internet and the mechanism for retrieving it.
The syntax of a typical URL is:
scheme://domain:port/path?query_string#fragment_id
In this problem, the scheme, domain is required by all URL and other components are optional. That is, for example, the following are all correct urls:
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)
ftp://fs.fudan.edu.cn/
telnet://bbs.fudan.edu.cn/
http://mail.bashu.cn:8080/BsOnline/
Your task is to find the domain for all given URLs.
 

Input
There are multiple test cases in this problem. The first line of input contains a single integer denoting the number of test cases.
For each of test case, there is only one line contains a valid URL.
 

Output
For each test case, you should output the domain of the given URL.
 

Sample Input
3 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
 

Sample Output
Case #1: dict.bing.com.cn Case #2: www.mariowiki.com Case #3: mail.google.com
 

Source
这道题不难,但是我第一次交的时候wa了。题意看错了,以为只输出://    /之间的字符。看别人的博客,人家代码的意思是不但可以输出://   /之间的字符,如果是只有:的话,输出://   :之间的话。。我写的,如果:  和/都有的话,谁在前面,就输出:  和这个在前面字符的之间的字符。。结果就ac 了。。
我的代码:
#include<iostream>
#include<string>
using namespace std;
int main()
{
    int T,start,end1,end2,i,count=1;
    string str;
    cin>>T;
    while(T--)
    {
       cin>>str;
       start=str.find("://");
       end1=str.find('/',start+3);
       end2=str.find(':',start+3);
       printf("Case #%d: ",count++);
       if(end2!=-1 && end1!=-1)
       {
           if(end1<end2)
       {  for(i=start+3;i<end1;i++)
           cout<<str[i];
        cout<<endl;
       }
      if(end1>end2)
       {
           for(i=start+3;i<end2;i++)
           cout<<str[i];
       cout<<endl;
       }  
       }
       if(end1==-1)
       { for(i=start+3;i<end2;i++)
           cout<<str[i];
       cout<<endl;}
       if(end2==-1)
       {
             for(i=start+3;i<end1;i++)
           cout<<str[i];
       cout<<endl;
       }
    }
    return 0;
}

Parsing URL

标签:

原文地址:http://blog.csdn.net/zuguodexiaoguoabc/article/details/43836689

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!