Fold large input lines into smaller ones

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












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 <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int length(char input);

int main(int argc, char* argv)


if(argc < 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) <= length(argv[1]))


//print remaining characters
int printed = 0;
++numOfCuts;
for(int i=startCol; i <= length(argv[1]); ++i)

if(isspace(argv[1][i]) && 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] != ''; ++i)
;
return i;







share|improve this question

















  • 1




    Why does code covert the string int COL_BOUND = atof(argv[2]); to a float and then assigned to an int?
    – chux
    May 11 at 2:41






  • 2




    I am not proficient on reading input from the console, as I've noted above. The reason its assigned to an int is because it is used as an integer value - the column bound. A column bound would never have a decimal part to it; it's either on one character or the other. Edit: Upon further inspection, man atof revealed that its integer equivalent is indeed atoi. Thank you for pointing this out.
    – M. Lago
    May 11 at 2:47

















up vote
4
down vote

favorite












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 <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int length(char input);

int main(int argc, char* argv)


if(argc < 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) <= length(argv[1]))


//print remaining characters
int printed = 0;
++numOfCuts;
for(int i=startCol; i <= length(argv[1]); ++i)

if(isspace(argv[1][i]) && 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] != ''; ++i)
;
return i;







share|improve this question

















  • 1




    Why does code covert the string int COL_BOUND = atof(argv[2]); to a float and then assigned to an int?
    – chux
    May 11 at 2:41






  • 2




    I am not proficient on reading input from the console, as I've noted above. The reason its assigned to an int is because it is used as an integer value - the column bound. A column bound would never have a decimal part to it; it's either on one character or the other. Edit: Upon further inspection, man atof revealed that its integer equivalent is indeed atoi. Thank you for pointing this out.
    – M. Lago
    May 11 at 2:47













up vote
4
down vote

favorite









up vote
4
down vote

favorite











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 <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int length(char input);

int main(int argc, char* argv)


if(argc < 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) <= length(argv[1]))


//print remaining characters
int printed = 0;
++numOfCuts;
for(int i=startCol; i <= length(argv[1]); ++i)

if(isspace(argv[1][i]) && 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] != ''; ++i)
;
return i;







share|improve this question













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 <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int length(char input);

int main(int argc, char* argv)


if(argc < 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) <= length(argv[1]))


//print remaining characters
int printed = 0;
++numOfCuts;
for(int i=startCol; i <= length(argv[1]); ++i)

if(isspace(argv[1][i]) && 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] != ''; ++i)
;
return i;









share|improve this question












share|improve this question




share|improve this question








edited May 10 at 23:44
























asked May 10 at 23:37









M. Lago

385




385







  • 1




    Why does code covert the string int COL_BOUND = atof(argv[2]); to a float and then assigned to an int?
    – chux
    May 11 at 2:41






  • 2




    I am not proficient on reading input from the console, as I've noted above. The reason its assigned to an int is because it is used as an integer value - the column bound. A column bound would never have a decimal part to it; it's either on one character or the other. Edit: Upon further inspection, man atof revealed that its integer equivalent is indeed atoi. Thank you for pointing this out.
    – M. Lago
    May 11 at 2:47













  • 1




    Why does code covert the string int COL_BOUND = atof(argv[2]); to a float and then assigned to an int?
    – chux
    May 11 at 2:41






  • 2




    I am not proficient on reading input from the console, as I've noted above. The reason its assigned to an int is because it is used as an integer value - the column bound. A column bound would never have a decimal part to it; it's either on one character or the other. Edit: Upon further inspection, man atof revealed that its integer equivalent is indeed atoi. Thank you for pointing this out.
    – M. Lago
    May 11 at 2:47








1




1




Why does code covert the string int COL_BOUND = atof(argv[2]); to a float and then assigned to an int?
– chux
May 11 at 2:41




Why does code covert the string int COL_BOUND = atof(argv[2]); to a float and then assigned to an int?
– chux
May 11 at 2:41




2




2




I am not proficient on reading input from the console, as I've noted above. The reason its assigned to an int is because it is used as an integer value - the column bound. A column bound would never have a decimal part to it; it's either on one character or the other. Edit: Upon further inspection, man atof revealed that its integer equivalent is indeed atoi. Thank you for pointing this out.
– M. Lago
May 11 at 2:47





I am not proficient on reading input from the console, as I've noted above. The reason its assigned to an int is because it is used as an integer value - the column bound. A column bound would never have a decimal part to it; it's either on one character or the other. Edit: Upon further inspection, man atof revealed that its integer equivalent is indeed atoi. Thank you for pointing this out.
– M. Lago
May 11 at 2:47











1 Answer
1






active

oldest

votes

















up vote
0
down vote













The length() function can be replaced using strlen() (you'll need to include <string.h>). 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 < 3) 
fprintf(stderr, "Usage: %s input colsn", 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], &parse_end, 10);
if (parse_end == argv[2] || *parse_end)
fprintf(stderr, "Usage: %s input colsn", 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 <, not <=:



while ((nextBound = startCol + col_bound - 1) < 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?






share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194156%2ffold-large-input-lines-into-smaller-ones%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    The length() function can be replaced using strlen() (you'll need to include <string.h>). 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 < 3) 
    fprintf(stderr, "Usage: %s input colsn", 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], &parse_end, 10);
    if (parse_end == argv[2] || *parse_end)
    fprintf(stderr, "Usage: %s input colsn", 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 <, not <=:



    while ((nextBound = startCol + col_bound - 1) < 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?






    share|improve this answer

























      up vote
      0
      down vote













      The length() function can be replaced using strlen() (you'll need to include <string.h>). 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 < 3) 
      fprintf(stderr, "Usage: %s input colsn", 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], &parse_end, 10);
      if (parse_end == argv[2] || *parse_end)
      fprintf(stderr, "Usage: %s input colsn", 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 <, not <=:



      while ((nextBound = startCol + col_bound - 1) < 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?






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        The length() function can be replaced using strlen() (you'll need to include <string.h>). 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 < 3) 
        fprintf(stderr, "Usage: %s input colsn", 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], &parse_end, 10);
        if (parse_end == argv[2] || *parse_end)
        fprintf(stderr, "Usage: %s input colsn", 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 <, not <=:



        while ((nextBound = startCol + col_bound - 1) < 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?






        share|improve this answer













        The length() function can be replaced using strlen() (you'll need to include <string.h>). 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 < 3) 
        fprintf(stderr, "Usage: %s input colsn", 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], &parse_end, 10);
        if (parse_end == argv[2] || *parse_end)
        fprintf(stderr, "Usage: %s input colsn", 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 <, not <=:



        while ((nextBound = startCol + col_bound - 1) < 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?







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jul 17 at 12:31









        Toby Speight

        17.4k13488




        17.4k13488






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194156%2ffold-large-input-lines-into-smaller-ones%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Chat program with C++ and SFML

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            Will my employers contract hold up in court?