将输入的空格串替换为最少数量的制表符和空格。
2016-11-23 00:02
176 查看
#include <stdio.h> #define MAXLINE 1000 /* max input line size */ #define TAB2SPACE 4 /* 4 spaces to a tab */ char line[MAXLINE]; /*current input line*/ int getline(void); /* taken from the KnR book. */ int main() { int i, t; int spacecount, len; while ((len = getline()) > 0) { spacecount = 0; for (i = 0; i < len; i++) { if (line[i] == ' ') spacecount++; /* increment counter for each space */ if (line[i] != ' ') spacecount = 0; /* reset counter */ if (spacecount == TAB2SPACE) /* Now we have enough spaces ** to replace them with a tab */ { /* Because we are removing 4 spaces and ** replacing them with 1 tab we move back ** three chars and replace the ' ' with a \t */ i -= 3; /* same as "i = i - 3" */ len -= 3; line[i] = '\t'; /* Now move all the char's to the right into the ** places we have removed. */ for (t = i + 1; t<len; t++) line[t] = line[t + 3]; /* Now set the counter back to zero and move the ** end of line back 3 spaces */ spacecount = 0; line[len] = '\0'; } } printf("%s", line); } return 0; } /* getline: specialized version */ int getline(void) { int c, i; extern char line[]; for (i = 0; i<MAXLINE - 1 && (c = getchar()) != EOF && c != '\n'; ++i) line[i] = c; if (c == '\n') { line[i] = c; ++i; } line[i] = '\0'; return i; }
相关文章推荐
- 空格串替换为最少数量的制表符和空格
- 练习1-21:编写程序entab,将空格串替换为最少数量的制表符和空格。。。(C程序设计语言 第2版)
- 练习1-21 编写程序entab,将空格串替换成最少数量的制表符和空格,但要保持单词之间的间隔不变。假设制表符终止位的位置与练习1-20的detab程序的情况相同。当使用一个制表符或者一个空格都可以到达下一个制表符终止位时,选用哪种替换字符比较好。
- 练习 1-21 将空格串替换为最少数量的制表符和空格
- the c programing language 练习1-21 将空格字符替换为最少数量的制表符和空格
- 将输入的制表符替换成适当数目的空格,使空格充满到下一个制表符终止的地方。
- 将输入的制表符替换成适当数目的空格,使空格充满到下一个制表符终止位的地方
- 将输入中的制表符替换成适当数目的空格,使空格充满到下一个制表符终止位的地方
- 编些程序detaba,将输入中的制表符替换为适当数目的空格,使空格充满到下一个制表符终止位的地方
- 练习1-20:请编写程序detab,将输入中的制表符替换成适当数目的空格,适空格充满到下一个制表符中止的地方。假设制表符终止位的位置是固定的,比如每隔n列就会出现一个制表符终止位。n应该作为变量还是符号常量呢?
- 将输入中的制表符替换成适当数目的空格,使空格充满到下一个制表符终止位的地方
- 练习 1-20 编写程序 detab,将输入中的制表符替换成适当数目的空格,使空格充满到 下一个制表符终止位的地方。
- 第7周 C语言程序设计(新2版) 练习1-21 空格串替换为最小数目的空格和制表符
- 从键盘输入一个英文句子,并处理:去掉最左边和最右边的空白(空格、制表符),把连续的空白替换为一个空格。
- 将输入中的制表符替换成适当数目的空格,使空格充满到下一个制表符终止位的地方
- C语言程序设计习题1-18 删除每个输入行末尾的空格或制表符,并删除完全是空格的行
- 编写程序detab,将输入中的制表符换成适当数目的空格
- 转义字符,替换原来输入的回车 换行 空格
- 请实现一个函数,把字符串中的每个空格替换成“%20”。例如输入“We are happy.”,则输出“We%20are%20happy.”。
- 输入一行字符,统计其中字母、数字、空格、其它字符的数量,并输出到控制台