poj_3650 字符串处理

使用<string>中字符串处理函数:

(1)、getline(istream &in,string &s):

–从输入流中读入字符,存到string变量
–直到出现以下情况为止:
•读入了文件结束标志
•读到一个新行
•达到字符串的最大长度

(2)、int position=string.find("xxx"):

返回string中首次出现串xxx的下标位置给position

如果position == s.npos ,说明没有找到,C++用一个特别的标志npos标识;

其它用法:

int position=string.find("xxx",5);

从string的下标5后开始查找;

(3)、string.replace(position,len,"xxx");

将串stringpos开始的位置,len长度的串由串xxx替代;


code:

<span style="font-size:18px;">#include <iostream>
#include <string>
#include <fstream>

using namespace std;

string replace[7][2] = {
	{ "%", "%25" },
	{ " ", "%20" },
	{ "!", "%21" },
	{ "$", "%24" },
	{ "(", "%28" },
	{ ")", "%29" },
	{ "*", "%2a" }
};

int main()
{
	//fstream in("input.txt");
	string s;
	while (getline(cin,s) && s!="#")
	{
		for (int i = 0; i < 7; i++)
		{
			int pos = s.find(replace[i][0]);
			while (pos != string::npos)
			{
				s.replace(pos,1,replace[i][1]);
				pos += 2;
				pos = s.find(replace[i][0],pos);
			}
		}
		cout << s << endl;
	}
	//system("pause");
	return 0;
}
</span>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值