-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtext-alignment.c
46 lines (43 loc) · 1.65 KB
/
text-alignment.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void main() {
int N;
char alignment[8];
fgets(alignment, 8, stdin);
scanf("%d", &N); fgetc(stdin);
char text[N][257];
int max_length = 0;
for (int i = 0; i < N; i++) {
fgets(text[i], 257, stdin);
if(text[i][strlen(text[i])-1] == '\n') text[i][strlen(text[i])-1] = 0;
max_length = (strlen(text[i]) > max_length) ? strlen(text[i]) : max_length;
}
if ( alignment[0] == 'L') { for (int i = 0; i < N; i++) printf("%s\n", text[i]); }
else if ( alignment[0] == 'R' || alignment[0] == 'C' ) {
for (int i = 0; i < N; i++) {
int spaces = (alignment[0] == 'R') ? max_length - strlen(text[i]) : (max_length - strlen(text[i]))/2;
for (int j = 0; j < spaces; j++) printf(" ");
printf("%s\n", text[i]);
}
}
else if ( alignment[0] == 'J' ) {
for (int i = 0; i < N; i++) {
int nwords = 1;
for (int j = 0; j < strlen(text[i]); ++j) { nwords += (text[i][j] == ' ') ? 1 : 0; }
if (nwords > 1) {
float total = 0;
int spaces = 0;
for (int j = 0; j < strlen(text[i]); ++j) {
if (text[i][j] == ' ') {
total += (max_length - strlen(text[i]) + nwords-1) / (nwords-1);
for (int k = spaces; k < (int)(total); ++k) { spaces++; printf(" "); }
}
else { printf("%c", text[i][j]); }
}
printf("\n");
}
else printf("%s\n", text[i]);
}
}
}