0050 - Apple and Peach

#include <iostream>
#include <string>
using namespace std;

void Slove(string str)	{
	string apple="apple", peach="peach";

	for (int i=0; i<str.size(); ++i)	{
		if (str.substr( i,apple.size() ) == apple)
			for (int j=0; j<peach.size(); ++j)
				str[i++]=peach[j];
		if (str.substr( i,peach.size() ) == peach)
			for (int j=0; j<apple.size(); ++j)
				str[i++]=apple[j];
	}
	cout << str << endl;
}

int main(void)	{
	string str;

	getline(cin,str);
	Slove(str);
	return 0;
}

部分文字列を取ってきて比較する。