The first line gives the number of names n, and each of the next n lines gives one English name. Use the length of the longest name as the width, and print every name right-aligned to that width, one per line in input order. Right-aligned means spaces are added in front of (to the left of) the name so its length equals the width. The longest name is printed as is, with no spaces.
Input
1 ≤ n ≤ 100. Names use English letters only, length 1 to 20
Output
n lines, each name padded on the left with spaces to the width
Example 1 · stdin
3
Ann
Maximilian
Jo
stdout
Ann
Maximilian
Jo
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
Padding on the left: Python s.rjust(w), JS/TS s.padStart(w), C# s.PadLeft(w), Java String.format("%" + w + "s", s), C printf("%*s", w, s) (the width is passed as an argument), C++ std::setw(w). Craykon s.PadLeft(w) (same as C#; "7".PadLeft(3, "0") gives 007). The left-aligning versions have similar names (Python ljust, C %-*s), so they are easy to mix up. Trap: you only know the width after reading every name. Printing while you read aligns to the widest name so far, and fixing the width at the maximum length 20 is also wrong.
Pythonmain.py · 5 lines
n = int(input())names = [input().strip() for _ in range(n)]width = max(len(name) for name in names)for name in names: print(name.rjust(width))
import java.io.BufferedReader;import java.io.InputStreamReader;public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); String[] names = new String[n]; int width = 0; for (int i = 0; i < n; i++) { names[i] = br.readLine().trim(); width = Math.max(width, names[i].length()); } StringBuilder out = new StringBuilder(); for (String name : names) out.append(String.format("%" + width + "s", name)).append('\n'); System.out.print(out); }}
C#Main.cs · 14 lines
using System;using System.Linq;class Program{ static void Main() { int n = int.Parse(Console.ReadLine()); string[] names = new string[n]; for (int i = 0; i < n; i++) names[i] = Console.ReadLine().Trim(); int width = names.Max(s => s.Length); foreach (string name in names) Console.WriteLine(name.PadLeft(width)); }}
C++main.cpp · 17 lines
#include <algorithm>#include <iomanip>#include <iostream>#include <string>#include <vector>int main() { int n; std::cin >> n; std::vector<std::string> names(n); size_t width = 0; for (auto& name : names) { std::cin >> name; width = std::max(width, name.size()); } for (const auto& name : names) std::cout << std::setw(static_cast<int>(width)) << name << "\n";}
Cmain.c · 15 lines
#include <stdio.h>#include <string.h>int main(void) { int n, width = 0; char names[100][21]; if (scanf("%d", &n) != 1) return 0; for (int i = 0; i < n; i++) { scanf("%20s", names[i]); int len = (int)strlen(names[i]); if (len > width) width = len; } for (int i = 0; i < n; i++) printf("%*s\n", width, names[i]); return 0;}
Craykonmain.cray · 13 lines
n = Console.ReadLine().ToNumber();names = [];width = 0;for (i = 0; i < n; i++){ name = Console.ReadLine().Trim(); names.Add(name); width = Mathf.Max(width, name.Length);}foreach (name in names){ Console.WriteLine(name.PadLeft(width));}