C++按单词换行的函数的代码-创新互联

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

创新互联建站是一家专注于网站建设、成都网站建设与策划设计,都昌网站建设哪家好?创新互联建站做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:都昌等地区。都昌做网站价格咨询:18980820575
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;

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前题目:C++按单词换行的函数的代码-创新互联
文章地址:http://pwwzsj.com/article/dhjigc.html