// 문자열 우측 공백문자 삭제 함수
char* rtrim(char* s)
{
    char t[MAX_STR_LEN];
    char* end;

    strcpy(t, s);            // Visual C 2003 이하에서는
    //strcpy_s(t, s);        // 이것은 Visual C 2005용

    end = t + strlen(t) - 1;
    while (end != t && isspace(*end))
        end--;

    *(end + 1) = '\0';
    s = t;

    return s;
}

// 문자열 좌측 공백문자 삭제 함수
char* ltrim(char* s)
{
    char* begin;
    begin = s;

    while (*begin != '\0')
    {
        if (isspace(*begin))
            begin++;
        else
        {
            s = begin;
            break;
        }
    }

    return s;
}

// 문자열 앞뒤 공백 모두 삭제 함수
char* trim(char *s)
{
    return rtrim(ltrim(s));
}

// 공백으로만 되어 있는 문자열에서 모든 공백 삭제
// 이렇게 해야 strlen으로 찍었을 때 길이가 0으로 나옴
char* atrim(char* s)
{
    char t[MAX_STR_LEN];
    char* end;

    strcpy(t, s);            // Visual C 2003 이하에서는
    //strcpy_s(t, s);        // 이것은 Visual C 2005용

    end = t + strlen(t) - 1;
    while (end != t-1 && isspace(*end))
        end--;

    *(end + 1) = '\0';
    s = t;

    return s;
}

'Dev > c, c++' 카테고리의 다른 글

이미지 오른쪽으로 90° 회전  (1) 2010.07.05
3차원 배열을 동적할당 해보자...  (3) 2008.03.28
대부분의 형을 문자열로 바꾸기  (0) 2008.03.18

+ Recent posts