Right-Justify Lines
Completed
Read a width w and n words. Print each word right-justified in w columns by printing w - strlen(word) spaces first (words longer than w are printed as they are).
Input
w n, then n words.
Output
n lines.
Example
Input
8 3 cat elephant ox
Output
cat
elephant
ox
Editor
C
Hints
Count how many spaces are needed, print them, then the word.
Solution explanation
Standard C also offers printf("%*s", w, word) for this, where the width comes from an argument.
Show solution
#include <stdio.h>
#include <string.h>
int main() {
int w, n;
char word[100];
scanf("%d %d", &w, &n);
for (int i = 0; i < n; i++) {
scanf("%s", word);
for (int s = strlen(word); s < w; s++) printf(" ");
printf("%s\n", word);
}
return 0;
}