Fold large input lines into smaller ones I am working on a program that is meant to fold a large input line into smaller ones. Basic flow of the program would be as follows:


User calls the program with a long string as input
Program prints tokens generated in separate lines


I haven't seen handling file input yet, so keep that in mind. I haven't seen reading input from the command line either.

Here's the source code:

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;ctype.h&gt;

int length(char input[]);

int main(int argc, char* argv[])
{

    if(argc &lt; 3)
    {
        printf("Usage: %s input [n]\n", argv[0]);
        exit(1);
    }

    int COL_BOUND = atof(argv[2]);

    int startCol, currentCut, nextBound, numOfCuts;

    startCol = currentCut = nextBound = numOfCuts = 0;

    //while the next bound is still "inside" string
    while((nextBound = startCol + COL_BOUND - 1) &lt;= length(argv[1]))
    {
        //find cut character
        for(int i = startCol; i &lt;= nextBound; ++i)
        {
            if(isspace(argv[1][i]) &amp;&amp; !isspace(argv[1][i-1]))
                currentCut = i - 1;
        }
        //there was no valid "cut" before whitespace or bound itself is valid
        if(!isspace(argv[1][nextBound]) || currentCut &lt;= startCol)
            currentCut = nextBound;

        //print [startCol, cut]
        int printed = 0;
        for(int i = startCol; i &lt;= currentCut; ++i)
        {
            if(isspace(argv[1][i]) &amp;&amp; printed == 0)
                continue;
            ++printed;
            putchar(argv[1][i]);
        }

        printf("\n");

        //adjust start column for the next run
        startCol = startCol + COL_BOUND;
        ++numOfCuts;
    }

    //print remaining characters
    int printed = 0;
    ++numOfCuts;
    for(int i=startCol; i &lt;= length(argv[1]); ++i)
    {
        if(isspace(argv[1][i]) &amp;&amp; printed == 0)
            continue;
        ++printed;
        putchar(argv[1][i]);
    }

    printf("\nOriginal string split into %d parts (bound was %d)\n", numOfCuts, COL_BOUND);
    return 0;
}

int length(char input[])
{
    int i;
    for(i=0; input[i] != '\0'; ++i)
        ;
    return i;
}
The length() function can be replaced using strlen() (you'll need to include &lt;string.h&gt;).  Note that strlen() doesn't consider the terminating NUL to be part of the string - but that will help fix the current bug that causes this code to print that NUL.

Don't measure the length repeatedly - instead, save it to a variable early on, and refer to that variable.



Error messages should go to standard error stream, not standard output:

if (argc &lt; 3) {
    fprintf(stderr, "Usage: %s input cols\n", argv[0]);
    return 1;
}




Don't use uppercase names for variables - we reserve those for macros; the "SHOUTING" of the name warns us that they need special care.

Also, don't use atof() for converting integers - use atoi(), or better, strtoul(), which can tell you whether it succeeded:

char *parse_end;
size_t col_bound = strtoul(argv[2], &amp;parse_end, 10);
if (parse_end == argv[2] || *parse_end) {
    fprintf(stderr, "Usage: %s input cols\n", argv[0]);
    return 1;
}




Prefer one declaration per line, and initialise immediately:

size_t startCol = 0;
size_t currentCut = 0;
size_t nextBound = 0;
size_t numOfCuts = 0;




Be careful about off-by-one errors.  This inequality should be &lt;, not &lt;=:

while ((nextBound = startCol + col_bound - 1) &lt; length) {


It's not the only one.



Test more inputs.  I tried a very simple case and got unexpected output:

./194156 'a b c d e f' 3


a b
c
d e
f
Original string split into 4 parts(bound was 3)


Why did it not split into 3 lines?