Fold large input lines into smaller ones
Clash 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;
c strings array
add a comment |Â
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;
c strings array
1
Why does code covert the stringint COL_BOUND = atof(argv[2]);
to afloat
and then assigned to anint
?
â 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 indeedatoi
. Thank you for pointing this out.
â M. Lago
May 11 at 2:47
add a comment |Â
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;
c strings array
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;
c strings array
edited May 10 at 23:44
asked May 10 at 23:37
M. Lago
385
385
1
Why does code covert the stringint COL_BOUND = atof(argv[2]);
to afloat
and then assigned to anint
?
â 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 indeedatoi
. Thank you for pointing this out.
â M. Lago
May 11 at 2:47
add a comment |Â
1
Why does code covert the stringint COL_BOUND = atof(argv[2]);
to afloat
and then assigned to anint
?
â 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 indeedatoi
. 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
add a comment |Â
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?
add a comment |Â
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?
add a comment |Â
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?
add a comment |Â
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?
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?
answered Jul 17 at 12:31
Toby Speight
17.4k13488
17.4k13488
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Why does code covert the string
int COL_BOUND = atof(argv[2]);
to afloat
and then assigned to anint
?â 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 indeedatoi
. Thank you for pointing this out.â M. Lago
May 11 at 2:47