Dev/C, C++
LPTSTR 자료형의 문자열 치환
newtype
2004. 7. 28. 20:55
UINT Replace( LPTSTR target, LPCTSTR source, LPCTSTR find, LPCTSTR replace )
{
UINT i, nCount=0;
BOOL bFind;
LPCTSTR ps, pf;
LPTSTR buf=NULL, pt = target;
size_t nLenSrc = _tcslen( source );
buf = new TCHAR[nLenSrc+1];
if ( buf == NULL )
return NULL;
_tcsncpy( buf, source, nLenSrc );
buf[nLenSrc] = 0;
for(ps=buf; *ps; ps++)
{
if ( find[0] == *ps )
{
bFind = TRUE;
for(pf=find+1, i=1; *pf; pf++, i++)
{
if ( *pf != ps[i] )
{
bFind = FALSE;
break;
}
}
// 찾았다!
if ( bFind )
{
for(pf=replace; *pf; pf++)
*(pt++) = *pf;
ps += (--i);
nCount++;
continue;
}
}
*(pt++) = *ps;
}
*pt = 0;
delete[] buf;
return nCount;
}
{
UINT i, nCount=0;
BOOL bFind;
LPCTSTR ps, pf;
LPTSTR buf=NULL, pt = target;
size_t nLenSrc = _tcslen( source );
buf = new TCHAR[nLenSrc+1];
if ( buf == NULL )
return NULL;
_tcsncpy( buf, source, nLenSrc );
buf[nLenSrc] = 0;
for(ps=buf; *ps; ps++)
{
if ( find[0] == *ps )
{
bFind = TRUE;
for(pf=find+1, i=1; *pf; pf++, i++)
{
if ( *pf != ps[i] )
{
bFind = FALSE;
break;
}
}
// 찾았다!
if ( bFind )
{
for(pf=replace; *pf; pf++)
*(pt++) = *pf;
ps += (--i);
nCount++;
continue;
}
}
*(pt++) = *ps;
}
*pt = 0;
delete[] buf;
return nCount;
}
반응형