C语言 · 空白格式化

标题:空白格式化

“空白格式化”具体做法是:去掉所有首尾空白;中间的多个空白替换为一个空格。所谓空白指的是:空格、制表符、回车符。

填空为:*p_to<*p_from;

 1 #include<stdio.h>
 2 #include<string.h>
 3 void f(char* from, char* to){
 4     char* p_from = from;//定义字符指针
 5     char* p_to = to;
 6     while(*p_from==' ' || *p_from=='	' || *p_from=='
')
 7         p_from++;
 8     do{
 9         if(*p_from==' ' || *p_from=='	' || *p_from=='
'){ 
10             do{
11                 p_from++;
12             }while(*p_from==' ' || *p_from=='	' || *p_from=='
');
13             if(*p_to<*p_from)
14                 *p_to++ = ' ';
15         }
16     }while(*p_to++ = *p_from++);
17 }
18 int main(){
19     char str[5000];
20     gets(str);
21     int len = strlen(str);
22     f(str,str);
23     printf("%s",str);
24     return 0;
25 }
原文地址:https://www.cnblogs.com/panweiwei/p/6682901.html