There are errors in the topic use case, so the following codes are all failed. Don't ask me why I know the use case is wrong
1962: word replacement
Time limit: 1 Sec memory limit: 32 MB
Flower offering: 98 solution: 50
[flower offering] [wreath] [TK question bank]
Title Description
Enter a string to end with carriage return (string length < = 100). The string consists of several words separated by a space. All words are case sensitive. Now you need to replace one word with another and output the string after the replacement.
input
Multiple sets of data. Each group of data input includes 3 lines,
Line 1 is the string s that contains more than one word,
Line 2 is the word a to be replaced, (length < = 100)
Line 3 is the word b where a will be replaced. (length < = 100)
s. A, B have no spaces at the front and back.
output
Each test data output has only one line,
Replace all the words a in s with a string after b.
sample input
I love Tian Qin
I
You
sample output
You love Tian Qin
Code one
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MaxN = 256;
int main()
{
#ifdef _DEBUG
ifstream cin("data.txt");
#endif // _DEBUG
string str, src, dst;
while (getline(cin, str))
{
getline(cin, src);
getline(cin, dst);
str = ' ' + str + ' ';
src = ' ' + src + ' ';
dst = ' ' + dst + ' ';
for (size_t pos = str.find(src); pos != std::string::npos; pos = str.find(src, pos + 1U))
{
str.replace(pos, src.size(), dst);
}
str.erase(0U, 1U);
str.erase(str.size() - 1U);
cout << str << endl;
}
#ifdef _DEBUG
cin.close();
system("pause");
#endif // _DEBUG
return 0;
}
Code two
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MaxN = 256;
int main()
{
#ifdef _DEBUG
ifstream cin("data.txt");
#endif // _DEBUG
char data[MaxN], BeRep[MaxN], Rep[MaxN], New[MaxN], k;
while (cin.getline(data, MaxN))
{
cin.getline(BeRep, MaxN);
cin.getline(Rep, MaxN);
k = 0;
for (int i = 0; data[i] != 0; ++i)
{
int j = 0;
for (; BeRep[j] != 0 && data[i + j] != 0 && BeRep[j] == data[i + j]; ++j);
if (BeRep[j] == 0 && (data[i+j]==' ' || data[i+j] == 0) && (i == 0 || data[i - 1] == ' '))
{
for (int n = 0; Rep[n] != 0; ++n)
New[k++] = Rep[n];
i += (j-1);
}
else New[k++] = data[i];
}
New[k] = 0;
cout << New << endl;
}
#ifdef _DEBUG
cin.close();
system("pause");
#endif // _DEBUG
return 0;
}
Code three
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MaxN = 256;
int main()
{
#ifdef _DEBUG
ifstream cin("data.txt");
#endif // _DEBUG
char data[MaxN], BeRep[MaxN], Rep[MaxN], tmp[MaxN][MaxN];
int WordN, k;
while (cin.getline(data, MaxN))
{
cin.getline(BeRep, MaxN);
cin.getline(Rep, MaxN);
WordN = 0;
k = 0;
for (int i = 0; data[i] != 0; ++i)
{
if (data[i] == ' ')
{
tmp[WordN][k] = 0;
++WordN;
k = 0;
}
else
tmp[WordN][k++] = data[i];
}
tmp[WordN][k] = 0;
if (!strcmp(tmp[0], BeRep))
cout << Rep;
else
cout << tmp[0];
for (int i = 1; i <= WordN; ++i)
{
if (!strcmp(tmp[i], BeRep))
cout << " " << Rep;
else cout << " " << tmp[i];
}
cout << endl;
}
#ifdef _DEBUG
cin.close();
system("pause");
#endif // _DEBUG
return 0;
}