D
DFS
I wanted to pad a line out to length N (say 25) and add a visual cue,
like so:
Pad this long line to 25 |
Pad this line to 25 |
Pad this to 25 |
So I wrote:
char *padToEnd(const char *lineToPad, char *paddedLine, int lineLen) {
strcpy(paddedLine,lineToPad);
for (int i = 1; i <= (lineLen - (strlen(lineToPad) + 1)); i++) {
strncat(paddedLine, " ", 1);
}
strncat(paddedLine, "|", 1);
return paddedLine;
}
Then I found:
printf("%-*s|\n", lineLen, lineToPad);
where * lets you substitute the value for line length, right into the
printf.
Identical output. Nice!
C is pretty cool... when it's not beating you up.
like so:
Pad this long line to 25 |
Pad this line to 25 |
Pad this to 25 |
So I wrote:
char *padToEnd(const char *lineToPad, char *paddedLine, int lineLen) {
strcpy(paddedLine,lineToPad);
for (int i = 1; i <= (lineLen - (strlen(lineToPad) + 1)); i++) {
strncat(paddedLine, " ", 1);
}
strncat(paddedLine, "|", 1);
return paddedLine;
}
Then I found:
printf("%-*s|\n", lineLen, lineToPad);
where * lets you substitute the value for line length, right into the
printf.
Identical output. Nice!
C is pretty cool... when it's not beating you up.