删除行字符串开头的空格、tab 、行结尾的空格、tab和换行

int deleteSpaceEnterTab(char* srcstr) {
    if (NULL == srcstr) {
        printf("parameter error\n");
        return -1;
    }
    /* 删除行尾的换行符 */
    int tmpLen = strlen(srcstr);
    while ('\r' == srcstr[tmpLen - 1] || '\n' == srcstr[tmpLen - 1]) {
        srcstr[tmpLen - 1] = '\0';
        --tmpLen;
    }

    /* 删除行尾的空格和tab*/
    tmpLen = strlen(srcstr);
    while (srcstr[tmpLen - 1] == ' ' || srcstr[tmpLen - 1] == ' ') {
        srcstr[tmpLen - 1] = '\0';
        --tmpLen;
    }

    /* 删除行首的空格和tab */
    tmpLen = strlen(srcstr);
    char* pstr = srcstr;
    while (*srcstr == ' ' || *srcstr == '   ') {
        ++srcstr;
    }

    /*  判断是否需要字符前移 */
    if (pstr == srcstr) {
        return 0;
    }
    /*  字符前移 */
    while ('\0' != *srcstr) {
        *pstr++ = *srcstr++;
    }
    *pstr = '\0';
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Chris83/p/9400677.html
今日推荐