The first line gives an integer k and the second line gives a line of text. Replace each English letter with the letter k places later in the alphabet. After z comes a again (after Z comes A), and uppercase stays uppercase, lowercase stays lowercase. Characters that are not English letters (digits, symbols, spaces) stay unchanged.
Input
0 ≤ k ≤ 1000. Text length 1 to 200, printable ASCII only (codes 32 to 126)
Output
the converted line
Example 1 · stdin
3
Hello, World! xyz
stdout
Khoor, Zruog! abc
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
Converting a character to a number and back differs by language: Python ord(c)/chr(n), JS/TS c.charCodeAt(0)/String.fromCharCode(n), Craykon Str.Code(c)/Str.FromCode(n). In Java, C#, C and C++ a char already is a number, so you subtract c - 'a' directly and cast back with (char). The formula is the same everywhere: (c - 'a' + k) % 26 + 'a'. Two traps: forget % 26 and letters past z turn into symbols like { (k goes up to 1000, so you need it), and shifting every character without checking for a letter also changes spaces and digits.
Pythonmain.py · 11 lines
k = int(input())s = input()out = []for c in s: if "a" <= c <= "z": out.append(chr((ord(c) - ord("a") + k) % 26 + ord("a"))) elif "A" <= c <= "Z": out.append(chr((ord(c) - ord("A") + k) % 26 + ord("A"))) else: out.append(c)print("".join(out))
JavaScriptmain.js · 11 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const k = Number(lines[0]);const s = lines[1].replace(/\r$/, "");const shift = (c, base) => String.fromCharCode(((c.charCodeAt(0) - base + k) % 26) + base);let out = "";for (const c of s) { if (c >= "a" && c <= "z") out += shift(c, 97); else if (c >= "A" && c <= "Z") out += shift(c, 65); else out += c;}console.log(out);
TypeScriptmain.ts · 12 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const k: number = Number(lines[0]);const s: string = lines[1].replace(/\r$/, "");const shift = (c: string, base: number): string => String.fromCharCode(((c.charCodeAt(0) - base + k) % 26) + base);let out = "";for (const c of s) { if (c >= "a" && c <= "z") out += shift(c, 97); else if (c >= "A" && c <= "Z") out += shift(c, 65); else out += c;}console.log(out);
JavaMain.java · 17 lines
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 k = Integer.parseInt(br.readLine().trim()); String s = br.readLine(); StringBuilder out = new StringBuilder(); for (char c : s.toCharArray()) { if (c >= 'a' && c <= 'z') out.append((char) ((c - 'a' + k) % 26 + 'a')); else if (c >= 'A' && c <= 'Z') out.append((char) ((c - 'A' + k) % 26 + 'A')); else out.append(c); } System.out.println(out); }}
C#Main.cs · 19 lines
using System;using System.Text;class Program{ static void Main() { int k = int.Parse(Console.ReadLine()); string s = Console.ReadLine(); var sb = new StringBuilder(); foreach (char c in s) { if (c >= 'a' && c <= 'z') sb.Append((char)((c - 'a' + k) % 26 + 'a')); else if (c >= 'A' && c <= 'Z') sb.Append((char)((c - 'A' + k) % 26 + 'A')); else sb.Append(c); } Console.WriteLine(sb.ToString()); }}
C++main.cpp · 16 lines
#include <iostream>#include <string>int main() { int k; std::string s; std::cin >> k; std::cin.ignore(1, '\n'); std::getline(std::cin, s); if (!s.empty() && s.back() == '\r') s.pop_back(); for (char& c : s) { if (c >= 'a' && c <= 'z') c = static_cast<char>((c - 'a' + k) % 26 + 'a'); else if (c >= 'A' && c <= 'Z') c = static_cast<char>((c - 'A' + k) % 26 + 'A'); } std::cout << s << "\n";}
Cmain.c · 17 lines
#include <stdio.h>#include <string.h>int main(void) { int k; char s[256]; if (scanf("%d ", &k) != 1) return 0; if (!fgets(s, sizeof s, stdin)) return 0; s[strcspn(s, "\r\n")] = '\0'; for (int i = 0; s[i]; i++) { char c = s[i]; if (c >= 'a' && c <= 'z') s[i] = (char)((c - 'a' + k) % 26 + 'a'); else if (c >= 'A' && c <= 'Z') s[i] = (char)((c - 'A' + k) % 26 + 'A'); } puts(s); return 0;}
Craykonmain.cray · 22 lines
k = Console.ReadLine().ToNumber();s = Console.ReadLine();a = Str.Code("a");bigA = Str.Code("A");out = "";foreach (c in s){ code = Str.Code(c); if (c >= "a" && c <= "z") { out = out + Str.FromCode((code - a + k) % 26 + a); } else if (c >= "A" && c <= "Z") { out = out + Str.FromCode((code - bigA + k) % 26 + bigA); } else { out = out + c; }}Console.WriteLine(out);