• 题目1.2.4 The Seven Percent Solution(C++)


    Problem Description
    Uniform Resource Identifiers (or URIs) are strings like http://icpc.baylor.edu/icpc/, mailto:foo@bar.org, ftp://127.0.0.1/pub/linux, or even just readme.txt that are used to identify a resource, usually on the Internet or a local computer. Certain characters are reserved within URIs, and if a reserved character is part of an identifier then it must be percent-encoded by replacing it with a percent sign followed by two hexadecimal digits representing the ASCII code of the character. A table of seven reserved characters and their encodings is shown below. Your job is to write a program that can percent-encode a string of characters.

    Character  Encoding
    " " (space)  %20
    "!" (exclamation point)  %21
    "$" (dollar sign)  %24
    "%" (percent sign)  %25
    "(" (left parenthesis)  %28
    ")" (right parenthesis)  %29
    "*" (asterisk)  %2a
     
    Input
    The input consists of one or more strings, each 1–79 characters long and on a line by itself, followed by a line containing only "#" that signals the end of the input. The character "#" is used only as an end-of-input marker and will not appear anywhere else in the input. A string may contain spaces, but not at the beginning or end of the string, and there will never be two or more consecutive spaces.
     
    Output
    For each input string, replace every occurrence of a reserved character in the table above by its percent-encoding, exactly as shown, and output the resulting string on a line by itself. Note that the percent-encoding for an asterisk is %2a (with a lowercase "a") rather than %2A (with an uppercase "A").
     
    Sample Input
    Happy Joy Joy!
    http://icpc.baylor.edu/icpc/
    plain_vanilla
    (**)
    ?
    the 7% solution
    #
     
    Sample Output
    Happy%20Joy%20Joy%21
    http://icpc.baylor.edu/icpc/
    plain_vanilla
    %28%2a%2a%29
    ?
    the%207%25%20solution
     

    本题核心:C++的字符串输入、输出等操作问题

    1.基本输入

    char buf[50];

    cin>>buf;

    这种方法输入会忽略最初的空白字符(换行符、空格符、制表符),而且会在下一个空格字符或换行符结束。

    2.getline() 不定长输入字符串

    string buf;  //在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作。

    getline(cin,buf);

    对于getline(),读入整行数据,使用回车键的换行符来确定输入结尾。

    3.getline()定长输入字符串

    char buf[255];

    cin.getline(buf,255);

    读入整行数据,使用回车键的换行符来确定输入结尾,不保存换行符,在存储字符串时,用空值字符来替换换行符。

    比如:

    cin.getline(buf,5)

    输入:abcde

    输出:abcd  (由于buf长度为5,输出时,最后一个为空值字符结尾,所以无法打印e)

    4.string类和c字符串之间的转换

    可以将C字符串存储在string类型的变量中。

    char a[]="Hello";

    string b;

    b=a;

    但是string对象不能自动的转换为C字符串,需要进行显式的类型转换,需要用到string类的成员函数c_str()

    例如:strcpy(a,b,c_str());

    5.字符串到数字的转换

    atoi函数获取一个C字符串参数,返回对应的int值。如果参数不与一个int值对应,atoi就会返回0.atoi函数在文件为cstdlib的库中。如果数字太大,不能转换为int类型的值,可以使用atoi将字符串转换为long类型的值

    atoi("1234");//返回整数1234

    atoi("#123");//返回0

    6.string类的大小

    string buf;

    auto len = buf.size();

    len的数据类型不确定是int型还是无符号类型,所以使用auto。(还待深究....)

    本题代码:

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string str;
        
        while (getline(cin,str) && str !="#")
        {
            auto len=str.size();
            for (int i=0; i<len; i++) {
                switch (str[i]) {
                    case ' ':cout<<"%20";break;
                    case '!':cout<<"%21";break;
                    case '$':cout<<"%24";break;
                    case '%':cout<<"%25";break;
                    case '(':cout<<"%28";break;
                    case ')':cout<<"%29";break;
                    case '*':cout<<"%2a";break;
                    default:
                        cout<<str[i];break;
                }
                
            }
            cout<<"
    ";
        }
        
        return 0;
    }
    技进乎艺,艺进乎道
  • 相关阅读:
    更改EBSserver域名/IP
    iOS Dev (60) 怎样实现 UITextView 中的 placeHolder
    程序猿的量化交易之路(29)--Cointrader之Tick实体(16)
    美团面试中被问到的问题汇总
    汇报措辞:你懂得如何向领导汇报吗(审阅、审批、审阅、批示、查阅)?
    九月份总结
    Android 编程之第三方开发 MaoZhuaWeiBo微博开发演示样例-1
    OLE操作Excel编译错误处理
    在 VS2008 下操作 Excel 的方法总结
    vs2008 ole excel
  • 原文地址:https://www.cnblogs.com/weekend/p/4954046.html
Copyright © 2020-2023  润新知