在csdn上大家寫出了各種方式-@-可是... 都太復雜了 如
1 :
void replace(char *pInput, char *pOutput, char *pSrc, char *pDst)
{
char *pi, *po, *p;
int nSrcLen, nDstLen, nLen;
pi = pInput;
po = pOutput;
nSrcLen = strlen(pSrc);
nDstLen = strlen(pDst);
p = strstr(pi, pSrc);
if(p)
{
while(p)
{
nLen = (int)(p - pi);
memcpy(po, pi, nLen);
memcpy(po + nLen, pDst, nDstLen);
pi = p + nSrcLen;
po = po + nLen + nDstLen;
p = strstr(pi, pSrc);
}
strcpy(po, pi);
}
else
{
strcpy(po, pi);
}
}
2
Fun(char *A, char *B, char *C)
{
int lenA = strlen(A);
int lenB = strlen(B);
int lenC = strlen(C);
int Count = 0;
int j = 0;
int k = 0;
while ((j < lenA) && (i < lenB))
{
if ( A[j] == B[i] || (A[j] - '32' == B[i]) || (A[j] + '32' == B[i]))
{
j++;
i++;
Count++;
}
else
{
Count = 0;
i = 0;
j = j - i +1;
}
}
if (j >= lenB)
{
pos = i - lenB;
}
for (;pos < lenB&& Count--; pos++)
{
A[pos] = C[k++];
}
}
3
void find_rep(char *a, char *b, char *c)
{
int lena, lenb, lenc;
lena = strlen(a);
lenb = strlen(b);
lenc = strlen(c);
std::cout << "a:/t" << a <<endl
<< "b:/t" << b << endl
<< "c:/t" << c <<endl;
std::cout << "len:/t" << lena << "/t" << lenb << "/t" << lenc << endl;
int i, j, k, sanum;
int istore=0;
for(i=0; i<lena;)
{
sanum=0;
for(j=0; j<lenb&&i<lena;)
{
if(a[i]==b[j]||a[i]+32==b[j]||a[i]-32==b[j])
{
i++;
j++;
sanum++;
}
else
{
i=i-j+1;
j=0;
sanum=0;
}
}
if(sanum==lenb)
{
istore=i-sanum; //orin i
j=0;
for(istore, j; j<lenc; j++,istore++)
a[istore] = c[j];
j=i;//now i
i=i+lenc-lenb; //next i
for(k=i; k<lena+lenc-lenb; k, j++;)
a[k] = a[j];
a[k] = '/0';
lena = strlen(a);
}
}
std::cout << a << endl;
//system("pause");
}
4 最可愛的就是一個哥們了 他寫的代碼好奇怪 號稱用了STL可是....
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <iterator>
using namespace std;
class Comp
{
public:
bool operator()( const char& Elem1, const char& Elem2 )
{
return ToUpper(Elem1) == ToUpper(Elem2);
}
private:
char ToUpper( const char& ch )
{
return ch>='a' && ch<='z' ? ch-'a' + 'A' : ch;
}
};
string SearchAndReplace(const string& src, const string& searchment, const string& replacement )
{
vector<string::const_iterator> idxs;
string::const_iterator it = search( src.begin(), src.end(),
searchment.begin(), searchment.end(), Comp() ) ;
for( ;
it!= src.end();
it=search( it, src.end(),
searchment.begin(), searchment.end(), Comp() ) )
{
idxs.push_back( it );
it += searchment.size();
}
string result;
string::const_iteratorit_s = src.begin();
for ( vector<string::const_iterator>::iterator it = idxs.begin(); it != idxs.end(); ++it )
{
string::const_iterator it_e = *it;
copy( it_s, it_e, back_inserter( result ) );
copy( replacement.begin(), replacement.end(), back_inserter( result ) );
it_s = it_e + searchment.size();
}
if ( it_s != src.end() )
copy ( it_s, src.end(), back_inserter( result ) );
return result;
}
int main()
{
cout<< fun( "aaAbb", "ab", "kkk" ) ;
getchar();
}
當當當當
正確答案在這里
string function(string a, string b, string c)
{
int n = b.size();
int pos = 0;
while( (pos = a.find(b,pos)) != -1)
{
a.replace( pos, n, c );
}
}
1 :
void replace(char *pInput, char *pOutput, char *pSrc, char *pDst)
{
char *pi, *po, *p;
int nSrcLen, nDstLen, nLen;
pi = pInput;
po = pOutput;
nSrcLen = strlen(pSrc);
nDstLen = strlen(pDst);
p = strstr(pi, pSrc);
if(p)
{
while(p)
{
nLen = (int)(p - pi);
memcpy(po, pi, nLen);
memcpy(po + nLen, pDst, nDstLen);
pi = p + nSrcLen;
po = po + nLen + nDstLen;
p = strstr(pi, pSrc);
}
strcpy(po, pi);
}
else
{
strcpy(po, pi);
}
}
2
Fun(char *A, char *B, char *C)
{
int lenA = strlen(A);
int lenB = strlen(B);
int lenC = strlen(C);
int Count = 0;
int j = 0;
int k = 0;
while ((j < lenA) && (i < lenB))
{
if ( A[j] == B[i] || (A[j] - '32' == B[i]) || (A[j] + '32' == B[i]))
{
j++;
i++;
Count++;
}
else
{
Count = 0;
i = 0;
j = j - i +1;
}
}
if (j >= lenB)
{
pos = i - lenB;
}
for (;pos < lenB&& Count--; pos++)
{
A[pos] = C[k++];
}
}
3
void find_rep(char *a, char *b, char *c)
{
int lena, lenb, lenc;
lena = strlen(a);
lenb = strlen(b);
lenc = strlen(c);
std::cout << "a:/t" << a <<endl
<< "b:/t" << b << endl
<< "c:/t" << c <<endl;
std::cout << "len:/t" << lena << "/t" << lenb << "/t" << lenc << endl;
int i, j, k, sanum;
int istore=0;
for(i=0; i<lena;)
{
sanum=0;
for(j=0; j<lenb&&i<lena;)
{
if(a[i]==b[j]||a[i]+32==b[j]||a[i]-32==b[j])
{
i++;
j++;
sanum++;
}
else
{
i=i-j+1;
j=0;
sanum=0;
}
}
if(sanum==lenb)
{
istore=i-sanum; //orin i
j=0;
for(istore, j; j<lenc; j++,istore++)
a[istore] = c[j];
j=i;//now i
i=i+lenc-lenb; //next i
for(k=i; k<lena+lenc-lenb; k, j++;)
a[k] = a[j];
a[k] = '/0';
lena = strlen(a);
}
}
std::cout << a << endl;
//system("pause");
}
4 最可愛的就是一個哥們了 他寫的代碼好奇怪 號稱用了STL可是....
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <iterator>
using namespace std;
class Comp
{
public:
bool operator()( const char& Elem1, const char& Elem2 )
{
return ToUpper(Elem1) == ToUpper(Elem2);
}
private:
char ToUpper( const char& ch )
{
return ch>='a' && ch<='z' ? ch-'a' + 'A' : ch;
}
};
string SearchAndReplace(const string& src, const string& searchment, const string& replacement )
{
vector<string::const_iterator> idxs;
string::const_iterator it = search( src.begin(), src.end(),
searchment.begin(), searchment.end(), Comp() ) ;
for( ;
it!= src.end();
it=search( it, src.end(),
searchment.begin(), searchment.end(), Comp() ) )
{
idxs.push_back( it );
it += searchment.size();
}
string result;
string::const_iteratorit_s = src.begin();
for ( vector<string::const_iterator>::iterator it = idxs.begin(); it != idxs.end(); ++it )
{
string::const_iterator it_e = *it;
copy( it_s, it_e, back_inserter( result ) );
copy( replacement.begin(), replacement.end(), back_inserter( result ) );
it_s = it_e + searchment.size();
}
if ( it_s != src.end() )
copy ( it_s, src.end(), back_inserter( result ) );
return result;
}
int main()
{
cout<< fun( "aaAbb", "ab", "kkk" ) ;
getchar();
}
當當當當
正確答案在這里
string function(string a, string b, string c)
{
int n = b.size();
int pos = 0;
while( (pos = a.find(b,pos)) != -1)
{
a.replace( pos, n, c );
}
}