回复 10楼 wp231957
这。。。。。
程序代码:
/*
Description
在C语言中,将ASCII字符集中的制表符('\t')、回车符('\r')、换行符('\n')、垂直制表符('\v')、换页符('\f')和空格字符(' ')称作空白符。
你的任务是读入每行字符串,去掉行首和行尾的连续空白符,但是在任意非空白符中间的空白符不要去除。
Input
输入为多行,每行为一个串(不超过100个字符),至某行输入的非空白符仅为“END”结束。
Output
输出为多行,为每行输入的去掉前后空白符的串。“END”也输出。
Sample Input
abcdefg
12345678
XYZ
abc 123
END
Sample Output
abcdefg
12345678
XYZ
abc 123
END
HINT
头文件ctype.h中有isspace函数对空白符进行检测,若一个字符为空白符之一则返回真。
Append Code
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "my_tools.h"
const char* str_trim(char* s);
int main(int argc, char* argv[])
{
const char* filename = "Text.txt";
const size_t max_length = 100;
FILE* file;
errno_t errno = fopen_s(&file, filename, "rt");
if (errno != 0)
{
Pause("Data file open failure!");
return 1;
}
while (!feof(file))
{
char buffer[max_length + 1];
if (fgets(buffer, max_length, file) != NULL)
{
const char* result = str_trim(buffer);
printf_s("%s\n", result);
if (strcmp(result, "END") == 0)
{
break;
}
}
}
fclose(file);
Pause("Press any key to continue...");
}
const char* str_trim(char* s)
{
if (*s == '\0')
{
return s;
}
char* pStart = s;
char* pEnd = s + (strlen(s) - 1);
while ((*pStart != '\0') && isspace(*pStart++))
{
;
}
if (*pStart-- != '\0')
{
while (isspace(*pEnd--))
{
;
}
*(pEnd + 2) = '\0';
}
else
{
*pStart = '\0';
}
return pStart;
}

