C++按单词换行的函数的代码-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
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://scgulin.cn/article/gisceg.html