Align columns: Difference between revisions
Content added Content deleted
m (added related tasks.) |
|||
Line 4,429: | Line 4,429: | ||
Output with example text : |
Output with example text : |
||
[[File:centeredtext.png]] |
[[File:centeredtext.png]] |
||
=={{header|MATLAB}} / {{header|Octave}}== |
|||
center-justified formatting is not implemented here |
|||
<lang Matlab> |
|||
function r = align_columns(f) |
|||
fid = fopen('align_column_data.txt', 'r'); |
|||
D = {}; |
|||
M = 0; |
|||
while ~feof(fid) |
|||
s = fgetl(fid); |
|||
strsplit(s,'$'); |
|||
m = diff([0,find(s=='$')])-1; |
|||
M = max([M,zeros(1,length(m)-length(M))], [m,zeros(1,length(M)-length(m))]); |
|||
D{end+1}=s; |
|||
end |
|||
fclose(fid); |
|||
fprintf(1,'%%-- right-justified --%%\n') |
|||
FMT = sprintf('%%%ds ',M); |
|||
for k=1:length(D) |
|||
d = strsplit(D{k},'$'); |
|||
fprintf(1,FMT,d{:}); |
|||
fprintf(1,'\n'); |
|||
end |
|||
fprintf(1,'%%-- left-justified --%%\n') |
|||
FMT = sprintf('%%-%ds ',M); |
|||
for k=1:length(D) |
|||
d = strsplit(D{k},'$'); |
|||
fprintf(1,FMT,d{:}); |
|||
fprintf(1,'\n'); |
|||
end |
|||
end; |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
align_columns |
|||
%-- right-justified --% |
|||
Given a text file of many lines, where fields within a line |
|||
are delineated by a single 'dollar' character, write a program |
|||
that aligns each column of fields by ensuring that words in each |
|||
column are separated by at least one space. |
|||
Further, allow for each word in a column to be either left |
|||
justified, right justified, or center justified within its column. |
|||
%-- left-justified --% |
|||
Given a text file of many lines, where fields within a line |
|||
are delineated by a single 'dollar' character, write a program |
|||
that aligns each column of fields by ensuring that words in each |
|||
column are separated by at least one space. |
|||
Further, allow for each word in a column to be either left |
|||
justified, right justified, or center justified within its column. |
|||
</pre> |
|||
=={{header|ML/I}}== |
=={{header|ML/I}}== |