/*-------------------------------------------------------------
タブをスペースに変換する(ポインタ版)
--------------------------------------------------------------*/
#include <stdio.h>
void tab_space(char *pd, char *ps, int w); /*変換を行う関数*/
void main()
{
char a[]="\tabc\t123"; /*変換する文字列*/
char b[80]; /*結果を格納する配列*/
int tab_width; /*タブ幅*/
/*タブ幅の入力*/
printf("tab width : ");
scanf("%d", &tab_width);
/*変換処理*/
tab_space(b, a, tab_width);
/*結果の表示*/
printf("b[]==%s\n", b);
}
/*--------------------------------------------------------------------
変換を行う関数
---------------------------------------------------------------------*/
void tab_space(char *pd, char *ps, int w)
/* pd : 変換結果を格納するポインタ*/
/* ps : 元の文字列へのポインタ*/
/* w : タブ幅*/
{
int next_tab; /*次のタブ位置*/
int i, j; /*配列の添え字*/
for (i=0,j=0; *(ps+i) != '\0'; i++){
if (*(ps+i) == '\t'){
/*次のタブ位置の前まで' 'を詰める*/
next_tab = (j/w+1) * w;
for (; j<next_tab; j++) * (pd+j)=' ';
}
else
/*そのままコピー*/
*(pd+(j++)) = *(ps+i);
}
*(pd+j) = '\0';
}