我想在開始和結束時沒有空格的打印行。 我不明白爲什麼從結束刪除不起作用。從行尾刪除空格
#include <stdio.h>
#define MAX_LINE_LENGTH 1000
#define LINE_BEGIN 0
#define LINE_MIDDLE 1
#define INBLANK 2
void deleteBlankFromEnd(char line[], int offset);
void deleteLine(char line[], int offset);
main()
{
int c, i, status ;
i = status = 0;
char line[MAX_LINE_LENGTH];
while((c = getchar()) != EOF) {
if (c == ' ' || c == '\t') {
if (status == LINE_MIDDLE || status == INBLANK) {
line[i++] = c;
if (status == LINE_MIDDLE)
status = INBLANK;
}
} else if (c == '\n') {
if (status > 0) {
if (status == INBLANK) {
printf("Line length = %d ", i);
deleteBlankFromEnd(line, i);
}
printf("%s", line);
printf("End\n");
deleteLine(line, i);
}
i = 0;
status = LINE_BEGIN;
} else {
line[i++] = c;
status = LINE_MIDDLE;
}
}
}
void deleteBlankFromEnd(char line[], int offset) {
while (line[offset] == ' ' || line[offset] == '\t') {
line[offset--] = 0;
}
printf("Line length = %d ", offset);
}
void deleteLine(char line[], int offset) {
while (offset >= 0) {
line[offset--] = 0;
}
}