ZOJ 1243 URLs

查看原题

题意

字符串分离

思路

用sscanf实现分离,事实上也可以用string数组老老实实挑出分隔符,逻辑我处理不好,换成sscanf了。
把字符串先以://分成两部分,对后面分别进行处理。
我之前直接用一句sscanf(s,"%[a-zA-Z]://%[a-z,.,-]:%[0-9]/%[a-z,.,-]",protocol,host,port,path);到了第三例的时候崩了,同学提醒这样写表示里面的符号必需,所以是会出错的。以后注意。
关于sccanf的介绍

代码

#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    char s[61]={'\0'};
    for(int i=1;i<=n;i++){
        cin>>s;
        char protocol[60]={'\0'},
        host[60]={'\0'},
        port[60]={'\0'},
        path[60]={'\0'},
        temp[60]={'\0'},
        temp1[60]={'\0'};
        cout<<"URL #"<<i<<endl;
        sscanf(s,"%[a-zA-Z]://%s",protocol,temp);
        sscanf(temp,"%[^/]/%s",temp1,path);
        sscanf(temp1,"%[^:]:%s",host,port);
        if(!strcmp(protocol,""))
            cout<<"Protocol = <default>"<<endl;
        else
            cout<<"Protocol = "<<protocol<<endl;
        if(!strcmp(host,""))
            cout<<"Host     = <default>"<<endl;
        else
            cout<<"Host     = "<<host<<endl;
        if(!strcmp(port,""))
            cout<<"Port     = <default>"<<endl;
        else
            cout<<"Port     = "<<port<<endl;
        if(!strcmp(path,""))
            cout<<"Path     = <default>"<<endl;
        else
            cout<<"Path     = "<<path<<endl;
        cout<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值