{ }codelper
string indexingP0283

Letters picked along the diagonal

A note has n words written one per line. From the first word you pick its first letter, from the second word its second letter, and so on: from the i-th word you pick the i-th letter. If the i-th word is shorter than i letters, so there is no letter to pick, write * instead of a letter. The first line gives n, and each of the next n lines gives one word. Print the n characters you got, in order starting from the first word, joined without spaces on one line.

Input
1 ≤ n ≤ 100, each word is a string of 1 to 100 lowercase English letters
Output
One line with a string of n characters (lowercase letters and *)
Example 1 · stdin
4
cat
bone
do
gold
stdout
co*d
main.py
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

Character positions start at 0 in all 8 languages.
So if the loop variable i starts at 0, you pick word[i] from word number i (the (i + 1)-th when a person counts), and the valid positions are 0 to length - 1.
Picking: Python, JS, TS, C#, C, C++ and Craykon word[i], Java word.charAt(i).
Length: Python len(word), JS/TS word.length, Java word.length(), C# word.Length, C++ word.size(), C strlen(word), Craykon word.Length.
What happens when you read a position out of range differs a lot.
Python stops with IndexError, Java with StringIndexOutOfBoundsException, C# with IndexOutOfRangeException, and Craykon also stops, saying the string only has positions 0 to 2.
word[i] in JS and TS does not stop but gives undefined, so the text "undefined" gets attached to the answer (word.charAt(i) gives an empty string).
C does not check and hands back a letter left in the array by an earlier word, or the end marker \0.
C++ word[i] does not check either; only word.at(i) throws std::out_of_range.
So ask i < length before picking.
Two traps: (1) Asking i <= length goes out of range for a word whose length is exactly i.
(2) Using the position a person counts (from 1) directly in word[i] gives the letter one place later.

Pythonmain.py · 9 lines
n = int(input())answer = ""for i in range(n):    word = input().strip()    if i < len(word):       # 번호는 0부터라서 i째 낱말의 글자 번호는 0 ~ len - 1        answer += word[i]    else:        answer += "*"print(answer)
JavaScriptmain.js · 9 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);let answer = "";for (let i = 0; i < n; i++) {  const word = lines[i + 1].trim();  // 번호는 0부터라서 쓸 수 있는 글자 번호는 0 ~ length - 1  answer += i < word.length ? word[i] : "*";}console.log(answer);
TypeScriptmain.ts · 10 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);let answer = "";for (let i = 0; i < n; i++) {  const word: string = lines[i + 1].trim();  // 번호는 0부터라서 쓸 수 있는 글자 번호는 0 ~ length - 1  answer += i < word.length ? word[i] : "*";}console.log(answer);
JavaMain.java · 17 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader; public class Main {    public static void main(String[] args) throws IOException {        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));        int n = Integer.parseInt(in.readLine().trim());        StringBuilder answer = new StringBuilder();        for (int i = 0; i < n; i++) {            String word = in.readLine().trim();            // 번호는 0부터라서 쓸 수 있는 글자 번호는 0 ~ length() - 1            answer.append(i < word.length() ? word.charAt(i) : '*');        }        System.out.println(answer);    }}
C#Main.cs · 18 lines
using System;using System.Text; class Program{    static void Main()    {        int n = int.Parse(Console.ReadLine());        var answer = new StringBuilder();        for (int i = 0; i < n; i++)        {            string word = Console.ReadLine().Trim();            // 번호는 0부터라서 쓸 수 있는 글자 번호는 0 ~ Length - 1            answer.Append(i < word.Length ? word[i] : '*');        }        Console.WriteLine(answer);    }}
C++main.cpp · 16 lines
#include <iostream>#include <string> int main() {    int n;    std::cin >> n;    std::string answer;    for (int i = 0; i < n; i++) {        std::string word;        std::cin >> word;        // 번호는 0부터라서 쓸 수 있는 글자 번호는 0 ~ size() - 1        if (i < static_cast<int>(word.size())) answer += word[i];        else answer += '*';    }    std::cout << answer << '\n';}
Cmain.c · 16 lines
#include <stdio.h>#include <string.h> int main(void) {    int n;    if (scanf("%d", &n) != 1) return 0;    char word[101];    for (int i = 0; i < n; i++) {        if (scanf("%100s", word) != 1) return 0;        // C는 범위를 검사해 주지 않으니 길이를 직접 물어야 한다        if (i < (int)strlen(word)) putchar(word[i]);        else putchar('*');    }    putchar('\n');    return 0;}
Craykonmain.cray · 16 lines
n = Console.ReadLine().ToNumber();answer = "";for (i = 0; i < n; i++){    word = Console.ReadLine().Trim();    // 번호는 0부터라서 쓸 수 있는 글자 번호는 0 ~ 길이 - 1    if (i < word.Length)    {        answer += word[i];    }    else    {        answer += "*";    }}Console.WriteLine(answer);