POJ单词替换问题(C++)

来源:百度知道 编辑:UC知道 时间:2024/05/11 13:47:22
http://poj.grids.cn/problem?id=2944

再看一下我怎么错了

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

int main()
{
char s[100];
cin.getline( s, 100 );
char a[100];
cin.getline( a, 100 );
char b[100];
cin.getline( b, 100 );

char *p = b;

while ( p != NULL )
{
p = strstr( s, a );
*p = *b;
}

cout << s << endl;

return 0;
}
#include <iostream>
using namespace std;
#include <cstring>
int main()
{
char s[10000];
cin.getline( s, 100 );
char a[100];
cin.getline( a, 100 );
char b[100];
cin.getline( b, 100 );
char *p = b;

while ( p != NULL )
{
p = strstr( s, a );
int la,lb,ls,dif;
la

#include <iostream>
using namespace std;
#include <cstring>
int main()
{
char s[10000];
cin.getline( s, 100 );
char a[100];
cin.getline( a, 100 );
char b[100];
cin.getline( b, 100 );
char *p = b;
p = strstr( s, a ); //这里使用strstr作为匹配单词有可能会出现错误
//例如
//his name
//is
//his
//程序会出现错误,如果要避免,必需自己写一个匹配单词的函数
while ( p != NULL )
{

int la,lb,ls,dif;
la = strlen( a );
lb = strlen( b );
ls = strlen( s );
dif = lb - la;
if ( dif >= 0 )
{
while ( p != &s[ ls ] )
{
s[ ls + dif ] = s[ ls ];
ls -= 1;
}
int i = 0;
for ( ; b[i] != '\0';) //替换单词这一部分写错了
*p++ = b[ i++ ];
ls = strlen( s );
i = 0;

}
if ( dif < 0 )
{

dif *= -1;
/*
while ( p