zoukankan      html  css  js  c++  java
  • 题目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;
    }
    技进乎艺,艺进乎道
  • 相关阅读:
    【咖啡の设备】便携式冰滴壶——Dripo 使用体验 Experience Report of Ice Drip Coffee Maker——Dripo
    【异常记录(八)】 This operation requires IIS integrated pipeline mode.
    【异常记录(七)】MVC:从客户端中检测到有潜在危险的 Request.Form 值 的解决方法 [转] A potentially dangerous Request.Form value was detected from the client
    SQL优化:清理生产环境中已失效字段基本步骤 SQL optimization: basic steps to clean up invalid fields in production environments
    [转] sql server 跨数据库调用存储过程 SQL server calls stored procedures cross-databases
    获取lambda表达式类型,获取attributes是注意事项
    [笔记] SQL性能优化
    MSSQL 重建索引(在线重建、控制最大处理器数 、MAXDOP )
    [笔记] SQL性能优化
    [笔记] SQL性能优化
  • 原文地址:https://www.cnblogs.com/weekend/p/4954046.html
Copyright © 2011-2022 走看看