C
c언어 substring
선원태
2014. 4. 21. 09:24
c에서는 substring을 제공하지 않기 때문에 직접 함수구현을 해줘야함.
char *SubStr( char *pnInput, int nStart, int nLen )
{
int nLoop ;
int nLength ;
char *pszOutPut ;
if( pnInput == NULL ){
return NULL ;
}
pszOutPut = (char *)malloc( sizeof(char) * nLen + 1 ) ;
nLength = strlen( pnInput ) ;
if( nLength > nStart + nLen ){
nLength = nStart + nLen ;
}
for( nLoop = nStart ; nLoop < nLength ; nLoop++ ){
pszOutPut[nLoop-nStart] = pnInput[nLoop] ;
}
pszOutPut[nLoop - nStart] = '\0' ;
return pszOutPut ;
}