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


Problem Description

Uniform Resource Identifiers (or URIs) are strings like http://icpc.baylor.edu/icpc/, mailto:[email protected], 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;
}
时间: 2024-10-11 11:55:42

题目1.2.4 The Seven Percent Solution(C++)的相关文章

(HDUStep 1.2.5)The Seven Percent Solution(字符串的替换)

题目: The Seven Percent Solution Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2637 Accepted Submission(s): 1562   Problem Description Uniform Resource Identifiers (or URIs) are strings like http:

zoj 2932 The Seven Percent Solution

The Seven Percent Solution Time Limit: 2 Seconds      Memory Limit: 65536 KB Uniform Resource Identifiers (or URIs) are strings like http://icpc.baylor.edu/icpc/, mailto:[email protected], ftp://127.0.0.1/pub/linux, or even just readme.txt that are u

POJ 3650 &amp; ZJU 2932 &amp; HDU 2719 The Seven Percent Solution(模拟)

题目链接: PKU:http://poj.org/problem?id=3650 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1931 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2719 Description Uniform Resource Identifiers (or URIs) are strings like http://icpc.baylor.edu

HDUOJ 2560 The Seven Percent Solution

#include<iostream> #include<stdlib.h> using namespace std; int main() { //第一个循环用于输入,遇到#停止 while (1) { //第二个循环用于单次输入的每个字符的判断 while (1) { char c; c = getchar(); //若遇到换行符,则结束此次输入 if (c == '\n' || c == EOF) { cout << endl; break; } //遇到#停止 i

leetcode题目思路以及部分解答(一)

为了进好公司这一个多月就得抽时间刷leetcode了..感觉这个OJ很不严谨...好多边界条件都没说清处..不过还好可以推测.唯一的好处就是不用自己编译调试,可以直接在网上显示出结果.当然,复杂一点的题目为了调试自己构建题目的结构也是很麻烦的...所以我发现提交里面错误好多.....再就是在笔记本上会时不时的变卡...每次提交都得等个3,4分钟才成功.要不就502错误... 我的题目按照通过率来.从通过率最高的题目开始讲解.每题不一定是最优解,都是我想得.仅供参考. 题目标题我都标好了.可以用c

递归,搜索,回溯,最优路径(线段)

基本信息 内存:520kB 时间:0ms 语言:G++ 题目链接http://bailian.openjudge.cn/practice/solution/4779752/ 解题报告: 1.输入表格时,由于这里有空格,不能用scanf函数. 2.gets(board[i]+1) 不要把第一列刷去. 3.回溯mark[y][x]=false;这里在Search玩后,一定要将mark[y][x]改为false.否则下一次的搜索将不会访问到这个点. 4.方向最好用数组形式表示. 5.最重要的一点是:这

关于u32中查找和定位最后到bit Number of 1 Bits

题目来源: https://leetcode.com/problems/number-of-1-bits/ 刷leetcode的时候发现了这个题目. 作为常年跑底层嵌入式的我,对于这种题目兴趣还是很浓厚的 class Solution { public: int hammingWeight(uint32_t n) { int cnt = 0; for(int i=0;i<32;i++){ if((n&(1<<i)) !=0){ cnt ++; } } return cnt; }

Leetcode 二分查找 Search Insert Position

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 Total Submissions: 41575 Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be i

LeetCode(7)Reverse Integer

题目如下: C++代码: class Solution { public: int reverse(int x) { if( overflow(x) == true) { return 0; } int result = 0; while (x!=0) { result = 10*result + x%10; x /= 10; } return result; } private: bool overflow(int x)//此题要保证返回的整数位整型,确定的是x是一个整型 { //故要判断x是