码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-11-10 21:03:38      阅读:434      评论:0      收藏:0      [点我收藏+]

标签:

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<<"\n";
    }
    
    return 0;
}

 

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

标签:

原文地址:http://www.cnblogs.com/weekend/p/4954046.html

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