'programming'에 해당되는 글 387건

  1. 2017.11.07 ltrim / rtrim / trim

반응형

 

 


char *ltrim(char *s)
{
    char sTemp[8192+1];

    int nLen = 0;
    int nIdx = 0;
    int nPos = 0;

    memset(sTemp, 0x00, sizeof(sTemp));

    nLen = strlen(s);
    nPos = 0;

    for( nIdx = 0; nIdx < nLen; nIdx++ ) {

        if(s[nIdx] == ' ' || s[nIdx] == '\t' || s[nIdx] == '\n' || s[nIdx] == '\r' || s[nIdx] == '\f' || s[nIdx] == '\v') {  /*** isspace() : space, tab, line_feed, carriage_return, form_feed, vertical_tab ***/
            nPos = nPos + 1;
        }
        else {
            break;
        }

    }

    memset(sTemp, 0x00, sizeof(sTemp));

    for( nIdx = nPos; nIdx < nLen; nIdx++ ) {
        sTemp[nIdx - nPos] = s[nIdx];
    }

    memcpy(s, sTemp, nLen);

    return s;

}

/******************************************************************************/

char *rtrim(char *s)
{
    int pos = strlen(s)-1;

    for (; pos>=0; pos-- ) {

        if(s[pos] == ' ' || s[pos] == '\t' || s[pos] == '\n' || s[pos] == '\r' || s[pos] == '\f' || s[pos] == '\v') {  /*** isspace() : space, tab, line_feed, carriage_return, form_feed, vertical_tab ***/
            s[pos] = 0x00;
        }
        else {
            return s;
        }

    }

    return s;

}

/******************************************************************************/

char *trim(char *s)
{

    return rtrim(ltrim(s));

}

/******************************************************************************/

 

 

/******************************************************************************/

char *LTrim(char *s)
{
    char* begin;

    begin = s;

    while (*begin != '\0') {

        if (isspace(*begin)) {
            begin++;
        }
        else {
            s = begin;
            break;
        }

    }

    return s;

}

/******************************************************************************/

char *RTrim(char *s)
{
    int pos = strlen(s)-1;

    for (; pos>0; pos-- ) {

        if(s[pos] == ' ' || s[pos] == '\t' || s[pos] == '\n' || s[pos] == '\r') {
            s[pos] = 0x00;
        }
        else {
            return s;
        }

    }

    return s;
}

/******************************************************************************/

char *LRTrim(char *s)
{
    char *p1;
    char *p2;

   p1 = LTrim(s);
   p2 = RTrim(p1);

   return p2;

}

/******************************************************************************/

 

 

 

반응형
Posted by 공간사랑
,