TAB键转换为4个空格

#include 
int isCharTab (char tab) {
    return (('\t' == tab) ? 1 : 0); 
}
int isStrTab (char* tab) {
    return (('\\' == *tab && 't' == *(tab+1)) ? 1 : 0); 
}
void fill4Space (char* pDst) {
    int i = 0;
    for (i = 0; i < 4; i++)
        *pDst++ = ' ';
}
int RepelaceTab (char* pSrc, char* pDst) {
    if (NULL == pSrc || NULL == pDst) {
        printf ("string is null, error~");
        return ;
    }   
    while (*pSrc) {
        if (isCharTab (*pSrc)) {
            fill4Space (pDst);
            pSrc++;
            pDst += 4;
        } else {
            if (isStrTab (pSrc)) {
                fill4Space (pDst);
                pSrc += 2;
                pDst += 4;
            } else
                *pDst++ = *pSrc++;
        }   
    }   
}

int main (void) {
    char pSrc[100] = {0};
    char pDst[100] = {0};
    fgets (pSrc, 100, stdin); // 考虑此句后可跟清理缓冲区的代码,清理掉\n
    printf ("原字符串:%s\n", pSrc);
    RepelaceTab (pSrc, pDst);
    printf ("新字符串:%s\n", pDst);
    return 0;
}

你可能感兴趣的:(C/C++)