첫 줄에 정수 k, 둘째 줄에 문자열 한 줄이 주어진다. 영어 글자마다 알파벳 순서로 k칸 뒤의 글자로 바꿔 출력하라. z 다음은 다시 a로 돌아가고(Z 다음은 A), 대문자는 대문자로, 소문자는 소문자로 남는다. 영어 글자가 아닌 문자(숫자·기호·빈칸)는 그대로 둔다.
입력
0 ≤ k ≤ 1000. 문자열 길이 1 ~ 200, 아스키 출력 가능 문자(코드 32~126)만
출력
바꾼 문자열 한 줄
예시 1 · stdin
3
Hello, World! xyz
stdout
Khoor, Zruog! abc
main.py
오류 목록00F8 ↓ · ⇧F8 ↑
입력(stdin)
실행하면 이 줄들이 차례로 input()·표준 입력으로 들어가요(실행 중에 묻지 않아요).
$ 준비됐어요
코드를 입력해 풀려면 PC에서 열어 주세요. 모바일에서는 문제와 풀이를 볼 수 있어요.
다른 언어로는?언어별 풀이 보기 · 8개 언어 풀이
글자를 수로, 수를 글자로 바꾸는 방법이 언어마다 달라요: Python ord(c)/chr(n), JS·TS c.charCodeAt(0)/String.fromCharCode(n), 크레이콘 글자.코드(c)/글자.글자로변환(n). Java·C#·C·C++은 char가 곧 수라서 c - 'a'로 바로 빼고, 다시 (char)로 바꿔요. 공식은 모두 같아요: (c - 'a' + k) % 26 + 'a'. 함정 둘: % 26을 빼먹으면 z 뒤가 {처럼 기호가 되고(k가 1000까지라 꼭 필요해요), 글자인지 묻지 않고 모든 문자를 밀면 빈칸·숫자까지 바뀌어요.
Pythonmain.py · 11 줄
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 줄
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 줄
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 줄
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 줄
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 줄
#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 줄
#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 줄
k = 콘솔.줄읽기().수로변환();s = 콘솔.줄읽기();a = 글자.코드("a");bigA = 글자.코드("A");out = "";각각 (c 에서 s){ code = 글자.코드(c); 만약 (c >= "a" && c <= "z") { out = out + 글자.글자로변환((code - a + k) % 26 + a); } 아니면 만약 (c >= "A" && c <= "Z") { out = out + 글자.글자로변환((code - bigA + k) % 26 + bigA); } 아니면 { out = out + c; }}콘솔.쓰기(out);