{ }codelper
charactersprinting without newlineP0045

Letter chain

Two English letters a and b are given on one line, separated by a single space.
Both are lowercase or both are uppercase.
Print the letters from a to b in alphabet order on one line, one letter at a time with "-" between them.
If a comes after b, go backwards (toward b).
If a and b are the same, print just that letter.
Do not put "-" at the start or the end.

Input
a, b: one English letter each, same case
Output
one line of letters and "-"
Example 1 · stdin
c g
stdout
c-d-e-f-g
Example 2 · stdin
E B
stdout
E-D-C-B
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

Treating a letter "like a number" works differently per language.
In C, char is a small integer, so c + 1 is the next letter and putchar(c) prints it.
C++, Java and C# also have a char type, but char + int gives an int, so Java System.out.print('c' + 1) and C++ std::cout << 'c' + 1 print "100" — cast back with (char)(c + 1) or store it in a char variable again.
Using an int loop variable and printing it as is also gives "99-100-101".
Python, JS and TS have no char type, only one-letter strings, so you go through the code: Python ord("a") / chr(97), JS/TS "a".charCodeAt(0) / String.fromCharCode(97), Craykon Str.Code("a") / Str.FromCode(97).
Skip that and JS "c" + 1 silently becomes "c1", while Python stops with a TypeError.
Writing without a newline: Python print(x, end=""), JS/TS process.stdout.write (console.log always ends the line), Java System.out.print, C# Console.Write, C putchar / printf, C++ std::cout << x, Craykon Console.Write.
The common trap is adding "-" after every letter, which leaves one "-" at the end — add it before every letter except the first, or collect the letters and use "-".join / join("-").
Don't forget the single newline at the end.
And remember the backwards case, a > b.

Pythonmain.py · 8 lines
a, b = input().split()# 글자 ↔ 코드: ord("a") == 97, chr(97) == "a"step = 1 if a <= b else -1for code in range(ord(a), ord(b) + step, step):    if code != ord(a):        print("-", end="")    print(chr(code), end="")print()
JavaScriptmain.js · 10 lines
const [a, b] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/);// 글자 ↔ 코드: "a".charCodeAt(0) === 97, String.fromCharCode(97) === "a"const from = a.charCodeAt(0);const to = b.charCodeAt(0);const step = from <= to ? 1 : -1;for (let code = from; code !== to + step; code += step) {  if (code !== from) process.stdout.write("-");  process.stdout.write(String.fromCharCode(code));}process.stdout.write("\n");
TypeScriptmain.ts · 11 lines
import { readFileSync } from "node:fs";const [a, b]: string[] = readFileSync(0, "utf8").trim().split(/\s+/);// 글자 ↔ 코드: "a".charCodeAt(0) === 97, String.fromCharCode(97) === "a"const from: number = a.charCodeAt(0);const to: number = b.charCodeAt(0);const step: number = from <= to ? 1 : -1;for (let code = from; code !== to + step; code += step) {  if (code !== from) process.stdout.write("-");  process.stdout.write(String.fromCharCode(code));}process.stdout.write("\n");
JavaMain.java · 17 lines
import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        char a = sc.next().charAt(0);        char b = sc.next().charAt(0);        int step = a <= b ? 1 : -1;        StringBuilder out = new StringBuilder();        // char + int 는 int 가 되므로 (char)로 되돌린다        for (char c = a; c != b + step; c = (char) (c + step)) {            if (c != a) out.append('-');            out.append(c);        }        System.out.println(out);    }}
C#Main.cs · 19 lines
using System; class Program{    static void Main()    {        string[] parts = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        char a = parts[0][0];        char b = parts[1][0];        int step = a <= b ? 1 : -1;        // char + int 는 int 가 되므로 (char)로 되돌린다        for (char c = a; c != b + step; c = (char)(c + step))        {            if (c != a) Console.Write('-');            Console.Write(c);        }        Console.WriteLine();    }}
C++main.cpp · 13 lines
#include <iostream> int main() {    char a, b;    std::cin >> a >> b;    int step = a <= b ? 1 : -1;    // c + step 은 int 라서 char 변수에 다시 담아야 글자로 찍힌다    for (char c = a; c != b + step; c += step) {        if (c != a) std::cout << '-';        std::cout << c;    }    std::cout << '\n';}
Cmain.c · 14 lines
#include <stdio.h> int main(void) {    char a, b;    scanf(" %c %c", &a, &b);    int step = a <= b ? 1 : -1;    /* C의 char는 작은 정수라 c + step 이 곧 다음 글자다 */    for (char c = a; c != b + step; c += step) {        if (c != a) putchar('-');        putchar(c);    }    putchar('\n');    return 0;}
Craykonmain.cray · 19 lines
글자들 = Console.ReadLine().Split(" ");처음 = Str.Code(글자들[0]);끝 = Str.Code(글자들[1]);걸음 = 1;if (처음 > 끝){    걸음 = -1;}코드 = 처음;while (코드 != 끝 + 걸음){    if (코드 != 처음)    {        Console.Write("-");    }    Console.Write(Str.FromCode(코드));    코드 += 걸음;}Console.WriteLine("");