POJ 1572 字符串替换

http://poj.org/problem?id=1572     

看了网上那么多行的C++代码的做法我也是被吓到了,其实用C++的string类函数很简单啊

关键是替换的字符串不能回头,不然有些测试数据,一个循环过后,你又回头重新再新串中去查,很可能就无限循环了。

1.假如定义一个string s[1],则定义了一个字符串数组:

string s[2];
s[1]="01234 5678";

cout<

输出0 10

如果用getline(cin,string*),则会读取整个一行作为一个字符串存储在string*中,包括空格。

2.             str3.erase(index,str1[i].length());//删除index的位置
                str3.insert(index,str2[i]);//插入到index位置


              比如str=abana str1=ban,str2=ny,则index返回1,从2开始删除,删除到str1的长度。

             之后str=ana,insert把str2插入下标1的位置,插入长度为str2的长度。


#include 
#include 
#include 
using namespace std;

int main()
{
    //freopen("in.txt","r",stdin);
    while(1)
    {
        int n;
        scanf("%d",&n);
        if(n==0)
            break;
        string str1[12];
        string str2[12];
        getchar();
        for(int i=0; i



转载于:https://www.cnblogs.com/mingrigongchang/p/6246264.html

你可能感兴趣的:(c/c++)