网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C++按单词换行的函数的代码

工作过程中,把写代码过程比较好的代码片段做个记录,如下的代码段是关于C++按单词换行的函数的代码,希望对码农也有用途。

成都创新互联公司自成立以来,一直致力于为企业提供从网站策划、网站设计、成都网站建设、成都做网站、电子商务、网站推广、网站优化到为企业提供个性化软件开发等基于互联网的全面整合营销服务。公司拥有丰富的网站建设和互联网应用系统开发管理经验、成熟的应用系统解决方案、优秀的网站开发工程师团队及专业的网站设计师团队。


   This function takes a string and an output buffer and a desired width. It then copies 
   the string to the buffer, inserting a new line character when a certain line
   length is reached.  If the end of the line is in the middle of a word, it will
   backtrack along the string until white space is found.

#include 
#include 

    int i = 0;
    int k, counter;

    while(i < strlen( string ) ) 
    {
        for ( counter = 1; counter <= line_width; counter++ ) 
        {
            if ( i == strlen( string ) ) 
            {
                buffer[ i ] = 0;
                return buffer;
            }
            buffer[ i ] = string[ i ];
            if ( buffer[ i ] == 'n' )
            {
                counter = 1; 
            }
            i++;
        }
        if ( isspace( string[ i ] ) ) 
        {
            buffer[i] = 'n';
            i++;
        } 
        else
        {
            for ( k = i; k > 0; k--) 
            {
                if ( isspace( string[ k ] ) ) 
                {
                    buffer[ k ] = 'n';
                    i = k + 1;
                    break;
                }
            }
        }
    }
    buffer[ i ] = 0;

    return buffer;

网站标题:C++按单词换行的函数的代码
文章路径:http://bjjierui.cn/article/gisceg.html

其他资讯