본문 바로가기

C

c언어 substring

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 ;

}

'C' 카테고리의 다른 글

헤더파일 중복 include 피하기  (0) 2013.09.24