C程序设计语言 (第二版) 练习1-21
练习 1-21 编写程序entab,将空格串替换为最少数量的制表符和空格,但要保持单词之间的间隔不变。
注意:代码在win32控制台运行,在不同的IDE环境下,有部分可能需要变更。
IDE工具:Visual Studio 2010
代码块:
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 1000
void entab(char s[], int lim){
char c;
int i;
printf("Enter String:\n");
for(i = 0; i < lim - 1 && (c = getchar()) != EOF; i++){
if(c == ' ' && s[i-1] == ' ' && s[i-2] == ' ' && s[i-3] == ' ' && i >= 3){
s[i-3] = '\t';
i -= 3;
}
else{
s[i] = c;
}
}
s[i] = '\0';
}
int main(){
char line[MAXLINE];
entab(line, MAXLINE);
printf("%s\n", line);
system("pause");
return 0;
}