Skip to content

Instantly share code, notes, and snippets.

@tomcha
Created April 3, 2015 10:42
Show Gist options
  • Select an option

  • Save tomcha/1a8a48b3104804f54000 to your computer and use it in GitHub Desktop.

Select an option

Save tomcha/1a8a48b3104804f54000 to your computer and use it in GitHub Desktop.
1-21
#include <stdio.h>
#define MAXCHAR 100
#define TABSTOP 6
void entab();
int main(){
int c;
char str[MAXCHAR];
int i, j;
i = 0;
while((c = getchar()) != EOF){
str[i++] = c;
if(c == '\n'){
entab(str);
for(j = 0; str[j] != '\n'; j++){
printf("%c", str[j]);
}
printf("\n");
i = 0;
}
}
}
void entab(char str[]){
int i;
int tno = 0;
int sno = 0;
int j = 0;
for(i = 0; str[i] != '\n'; i++){
if(str[i] == '\t'){
tno += 1;
}else if(str[i] == ' '){
sno += 1;
}else{
return;
}
}
tno += sno / TABSTOP;
sno = sno % TABSTOP;
// printf("t:%d\n", tno);
// printf("s:%d\n", sno);
for(;(j < tno) && j < MAXCHAR; j++){
str[j] = '\t';
}
for(; (j < sno) && j < MAXCHAR; j++){
str[j] = ' ';
}
str[j++] = '\n';
for(; j < MAXCHAR; j++){
str[j] = '\0';
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment