自己搞,带注释,适合初学者:C程序设计语言练习1-22 把较长的输入行折成短一些的两行或多行,折行的位置在输入行的第n列之前的最后一个非空格之后

C程序设计语言练习1-22 把较长的输入行折成短一些的两行或多行,折行的位置在输入行的第n列之前的最后一个非空格之后。要保证程序能够智能地处理输入行很长及在指定的列前没有空格或制表符时的情况。

编辑源文件

User@User-PC MINGW32 /d/mycode/c/krc
$ vim k0135.c

/* 英文文本自动换行处理 */
#include <stdio.h>
#define MAXLEN 1000 //字符串的最大长度
#define LINEWIDTH 20 //行宽,显示的每行不能超过此宽度
#define MAXBACK 5 //最多回退的字符数,超过则直接断行,使用中杠结尾
int readline(char str[]);
int doline(char str[],char strnew[]);
int main(void){
    int len;
    char str[MAXLEN],strnew[MAXLEN];
    //读入新的字符串,退出输入按Ctrl+d(Linux)或Ctrl+z(MINGW32)
    while((len=readline(str))>0){
        //str是原始字符串,strnew是处理后的字符串
        doline(str,strnew);
        //输出处理后的字符串
        printf("%s",strnew);
    }   
    return 0;
}
/*
读入一行输入的数据
*/
int readline(char str[]){
    int i,c;
    for(i=0;i<MAXLEN-1&&(c=getchar())!=EOF&&c!='\n';++i){
        str[i]=c;
    }
    if(c=='\n'){
        str[i]=c;
        ++i;
    }
    str[i]='\0';
    return i;
}
/*
处理一行输入的数据
*/
int doline(char str[],char strnew[]){
    int i=0;//原字符串的计数器
    int j=0;//新字符串的计数器
    int len=0;//新行的当前长度
    int c;//原字符串中的字符    
    int lastpos=0;//上一个空格或tab相对于新行行首的位置
    while((c=str[i])!='\0'){
        if(len>=LINEWIDTH-1){
            //在最后一个字符时会有三种处理情况,每种情况的len和lastpos都有差别,注意查看!
            if(c==' '||c=='\t'){
                //如果最后一个字符是空格或Tab,则直接换行
                //strnew[j]='A';//调试专用
                strnew[j]='\n';
                len=-1;//后面要++len,所以此处是-1
                lastpos=0;//lastpos=len+1
            }else if(len-lastpos>5){
                //最后一个单词过长则使用连字符 - 进行连接,而不是直接换行
                //strnew[j+1]='B';//调试专用
                strnew[j]='-';
                strnew[j+1]='\n';
                j+=2;
                strnew[j]=c;
                len=0;//\n后占用了一个字符,后面还有++len,所以此处的len是0
                lastpos=1;//lastpos=len+1
            }else{
                //只有当最后一个单词长度小于5时,才会被放到下一行
                strnew[j]=c;
                //strnew[j-len+lastpos]='C';//调试专用
                strnew[j-len+lastpos]='\n';
                lastpos=len-lastpos;//将\n之后的字符串算到下一个字符串中
                len=lastpos-1;//后面有++len,所以此处要-1
            }
//          printf("1j:%d,len:%d,lastpos:%d\n",j,len,lastpos);//调试专用
        }else{
            strnew[j]=c;
            if(c==' '||c=='\t'){
                lastpos=len;//保存上一个空格或Tab的位置
//              printf("2j:%d,len:%d,lastpos:%d\n",j,len,lastpos);//调试专用
            }
        }
        //三个变量都要自增
        ++i;
        ++j;
        ++len;
    }
    if(c=='\n'){
        strnew[j]=c;
        ++j;
    }
    strnew[j]='\0';
    return 0;
}

编译生成可执行文件

User@User-PC MINGW32 /d/mycode/c/krc
$ gcc -o k0135 k0135.c

运行测试

User@User-PC MINGW32 /d/mycode/c/krc
$ ./k0135

Yii is a free, open-source Web application development framework written in PHP5 that promotes clean, DRY design and encourages rapid development. It works to streamline your application development and helps to ensure an extremely efficient, extensible, and maintainable end product.
//上面是运行后的输入,回车后输出结果如下,已自动换行
Yii is a free,
open-source Web
application develop-
ment framework
written in PHP5
that promotes clean-
, DRY design and
encourages rapid
development. It
works to streamline
your application
development and
helps to ensure an
extremely efficient-
, extensible, and
maintainable end
product.
^Z

User@User-PC MINGW32 /d/mycode/c/krc
$

(全文完)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ycjnx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值