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.

This code snippet is created originally by Sean Hubbard , I simply am sharing what I learned. I thought it would be best to share his example. Mine is a bit too confusing to understand... his is a lot easier, trust me.

Comments
:)
#include <string.h>
#include <ctype.h>
 
char* word_wrap (char* buffer, char* string, int line_width) {
    int i = 0;
    int k, counter;
 
    while(i < strlen( string ) ) 
    {
        // copy string until the end of the line is reached
        for ( counter = 1; counter <= line_width; counter++ ) 
        {
            // check if end of string reached
            if ( i == strlen( string ) ) 
            {
                buffer[ i ] = 0;
                return buffer;
            }
            buffer[ i ] = string[ i ];
            // check for newlines embedded in the original input 
            // and reset the index
            if ( buffer[ i ] == '\n' )
            {
                counter = 1; 
            }
            i++;
        }
        // check for whitespace
        if ( isspace( string[ i ] ) ) 
        {
            buffer[i] = '\n';
            i++;
        } 
        else
        {
            // check for nearest whitespace back in string
            for ( k = i; k > 0; k--) 
            {
                if ( isspace( string[ k ] ) ) 
                {
                    buffer[ k ] = '\n';
                    // set string index back to character after this one
                    i = k + 1;
                    break;
                }
            }
        }
    }
    buffer[ i ] = 0;
 
    return buffer;

Hi...

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>

#define LINE_WIDTH  (15)
#define BUFFER_SIZE (1024)

char* word_wrap (
        char*              buffer,
        const unsigned int buffer_sz,
        const char*        string,
        const unsigned int string_len,
        const unsigned int max_line_width)
{
    assert(buffer         != NULL &&
           buffer_sz      > 0     &&
           string         != NULL &&
           string_len     > 0     &&
           max_line_width > 0);

    unsigned int i              = 0,
                 cur_line_width = 0;
    int          last_space     = -1;

    // Go past every character of the string, as long as
    // we'd have buffer space to store it in.
    for (i = 0; i < string_len && i < buffer_sz - 1; i++)
    {
        buffer[i] = string[i];      // Copy the character to our buffer.
        cur_line_width++;           // Our line just grew by one.

        if (isspace(buffer[i]))     // Keep track of our last whitespace.
            last_space = i;

        // The max line length is reached, and we've found a whitespace up until this point.
        if (cur_line_width > max_line_width && last_space >= 0)
        {
            buffer[last_space]  = '\n';           // Replace the space with a newline.
            cur_line_width      = i - last_space; // And update the length of the new line.
        }
    }

    // The string was copied. Terminate the buffer with a NULL character.
    buffer[i] = '\0';

    return buffer;
}

int main(void)
{
    char* test = "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.";
    char buffer[BUFFER_SIZE];

    printf("%s", word_wrap(buffer, BUFFER_SIZE, test, strlen(test), LINE_WIDTH));

    return 0;
}

Edited 3 Years Ago by Gonbe

Good one Gonbe :) I will try my best to learn from your snippet (I am new to C, i have been at it for about 2 days :D)

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.