Align columns/C: Difference between revisions
Content added Content deleted
(replaced with clean method) |
m (Fixed syntax highlighting.) |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
{{collection|Column Aligner}} |
{{collection|Column Aligner}} |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
char *str = |
const char *str = |
||
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$\n" |
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$\n" |
||
"are$delineated$by$a$single$'dollar'$character,$write$a$program\n" |
"are$delineated$by$a$single$'dollar'$character,$write$a$program\n" |
||
Line 11: | Line 11: | ||
"justified,$right$justified,$or$center$justified$within$its$column."; |
"justified,$right$justified,$or$center$justified$within$its$column."; |
||
void align(char *in, char alignment) |
void align(const char *in, char alignment) |
||
{ |
{ |
||
int col, i, l, r; |
int col, i, l, r; |
||
int w[1024] = {0}; |
int w[1024] = {0}; |
||
char *s |
const char *s; |
||
for (s = in, i = col = 0; s[i]; s += i + 1) { |
for (s = in, i = col = 0; s[i]; s += i + 1) { |
||
Line 22: | Line 22: | ||
if (i > w[col]) w[col] = i; |
if (i > w[col]) w[col] = i; |
||
if (col++ >= 1024) abort(); /* |
if (col++ >= 1024) abort(); /* artificial limit */ |
||
if (s[i] == '\n') col = 0; |
if (s[i] == '\n') col = 0; |
||
Line 56: | Line 56: | ||
puts("\n---- center ----"); align(str, 'c'); |
puts("\n---- center ----"); align(str, 'c'); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
Latest revision as of 16:23, 1 September 2022
Align columns/C is part of Column Aligner. You may find other members of Column Aligner at Category:Column Aligner.
#include <stdio.h>
const char *str =
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$\n"
"are$delineated$by$a$single$'dollar'$character,$write$a$program\n"
"that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$\n"
"column$are$separated$by$at$least$one$space.\n"
"Further,$allow$for$each$word$in$a$column$to$be$either$left$\n"
"justified,$right$justified,$or$center$justified$within$its$column.";
void align(const char *in, char alignment)
{
int col, i, l, r;
int w[1024] = {0};
const char *s;
for (s = in, i = col = 0; s[i]; s += i + 1) {
for (i = 0; s[i] && s[i] != '$' && s[i] != '\n'; i++);
if (i > w[col]) w[col] = i;
if (col++ >= 1024) abort(); /* artificial limit */
if (s[i] == '\n') col = 0;
if (!s[i]) break;
}
for (s = in, i = col = 0; s[i]; s += i + 1) {
for (i = 0; s[i] && s[i] != '$' && s[i] != '\n'; i++);
switch(alignment) {
case 'l': r = w[col] - i; break;
case 'c': r = (w[col] - i)/2; break;
case 'r': r = 0; break;
}
l = w[col++] - i - r + 1;
while (l--) putchar(' ');
printf("%.*s", i, s);
while (r--) putchar(' ');
if (s[i] != '$') {
putchar('\n');
col = 0;
}
if (!s[i]) break;
}
}
int main(void)
{
puts("\n---- right ----"); align(str, 'r');
puts("\n---- left ----"); align(str, 'l');
puts("\n---- center ----"); align(str, 'c');
return 0;
}