A sign shop copies the uppercase/lowercase shape of a pattern word the customer chose onto a new word.
The first line gives the number of word pairs n, and each of the next n lines gives a pattern word p and a target word w separated by one space.
The i-th character of w (counting from 0) follows the character of p at position (i modulo the length of p). So if p is shorter than w, the pattern of p repeats from the start.
If the p character it follows is an uppercase English letter, make the w character uppercase; if it is a lowercase English letter, make it lowercase.
If the p character it follows is a digit, leave the w character unchanged.
Digits in w always stay as they are whatever the pattern, and making an uppercase letter uppercase leaves it unchanged.
For each pair print the changed w on one line.
Input
1 ≤ n ≤ 1,000. p and w are 1 to 20 characters of uppercase and lowercase English letters and digits
Output
n lines, each the changed w
Example 1 · stdin
3
HeLLo worlds
Ab codelper
x9Y PIZZA42
stdout
WoRLdS
CoDeLpEr
pIZzA42
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
How you ask whether a character is uppercase, and how you change it, differs by language. Python m.isupper()/islower(), Java Character.isUpperCase, C# char.IsUpper and C/C++ <ctype.h>isupper/toupper work on one character; JS, TS and Craykon have no such function, so compare by range like m >= "A" && m <= "Z". A common trap is deciding uppercase with m == m.upper() (JS m === m.toUpperCase()). A digit is unchanged by uppercasing, so "7" counts as uppercase. For changing, Python upper(), JS/TS toUpperCase() and Craykon ToUpper() leave non-letters alone, so no worry there. In C, changing directly with w[i] - 32 breaks letters that are already uppercase and digits, so use toupper or check the range first. C++ std::toupper returns an int, so std::cout << std::toupper(c) prints a number like "65"; cast it back to char. Java String.toUpperCase() and C# char.ToUpper follow the locale (culture), and in Turkish i becomes İ, so when you handle only English letters Java Character.toUpperCase(char) or C# ToUpperInvariant is safer. The repeating position of the pattern is i % p.length.
Pythonmain.py · 12 lines
n = int(input())for _ in range(n): p, w = input().split() out = [] for i, ch in enumerate(w): m = p[i % len(p)] # 무늬가 짧으면 처음부터 되풀이 if m.isupper(): # "7".isupper()는 False — 숫자는 대문자도 소문자도 아니다 ch = ch.upper() elif m.islower(): ch = ch.lower() out.append(ch) print("".join(out))
JavaScriptmain.js · 18 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const isUpper = (c) => c >= "A" && c <= "Z";const isLower = (c) => c >= "a" && c <= "z";const out = [];for (let k = 1; k <= n; k++) { const [p, w] = lines[k].trim().split(" "); let s = ""; for (let i = 0; i < w.length; i++) { const m = p[i % p.length]; // "7" === "7".toUpperCase()도 참이라, 같은지로 대문자를 가리면 숫자가 대문자로 친다 if (isUpper(m)) s += w[i].toUpperCase(); else if (isLower(m)) s += w[i].toLowerCase(); else s += w[i]; } out.push(s);}console.log(out.join("\n"));
TypeScriptmain.ts · 18 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);const isUpper = (c: string): boolean => c >= "A" && c <= "Z";const isLower = (c: string): boolean => c >= "a" && c <= "z";const out: string[] = [];for (let k = 1; k <= n; k++) { const [p, w]: string[] = lines[k].trim().split(" "); let s = ""; for (let i = 0; i < w.length; i++) { const m: string = p[i % p.length]; if (isUpper(m)) s += w[i].toUpperCase(); else if (isLower(m)) s += w[i].toLowerCase(); else s += w[i]; } out.push(s);}console.log(out.join("\n"));
JavaMain.java · 25 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 n = Integer.parseInt(br.readLine().trim()); StringBuilder out = new StringBuilder(); for (int k = 0; k < n; k++) { String[] parts = br.readLine().trim().split(" "); String p = parts[0]; String w = parts[1]; for (int i = 0; i < w.length(); i++) { char m = p.charAt(i % p.length()); char c = w.charAt(i); // Character.toUpperCase(char)는 지역 설정과 상관없다(String.toUpperCase()는 지역 설정을 따른다) if (Character.isUpperCase(m)) c = Character.toUpperCase(c); else if (Character.isLowerCase(m)) c = Character.toLowerCase(c); out.append(c); } out.append('\n'); } System.out.print(out); }}
C#Main.cs · 27 lines
using System;using System.Text;class Program{ static void Main() { int n = int.Parse(Console.ReadLine().Trim()); var sb = new StringBuilder(); for (int k = 0; k < n; k++) { string[] parts = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); string p = parts[0], w = parts[1]; for (int i = 0; i < w.Length; i++) { char m = p[i % p.Length]; char c = w[i]; // char.ToUpper(c)는 지금 문화권을 따른다. 영어 글자만 다룰 때는 ToUpperInvariant가 안전하다 if (char.IsUpper(m)) c = char.ToUpperInvariant(c); else if (char.IsLower(m)) c = char.ToLowerInvariant(c); sb.Append(c); } sb.Append('\n'); } Console.Write(sb); }}
C++main.cpp · 21 lines
#include <cctype>#include <iostream>#include <string>int main() { std::ios::sync_with_stdio(false); int n; std::cin >> n; for (int k = 0; k < n; k++) { std::string p, w; std::cin >> p >> w; for (std::size_t i = 0; i < w.size(); i++) { unsigned char m = p[i % p.size()]; unsigned char c = w[i]; // std::toupper는 int를 돌려주므로 char로 되돌려 담는다 if (std::isupper(m)) w[i] = static_cast<char>(std::toupper(c)); else if (std::islower(m)) w[i] = static_cast<char>(std::tolower(c)); } std::cout << w << '\n'; }}
Cmain.c · 21 lines
#include <ctype.h>#include <stdio.h>#include <string.h>int main(void) { int n; scanf("%d", &n); for (int k = 0; k < n; k++) { char p[32], w[32]; scanf("%31s %31s", p, w); size_t lp = strlen(p); for (size_t i = 0; w[i] != '\0'; i++) { unsigned char m = (unsigned char)p[i % lp]; /* toupper·tolower는 영어 글자가 아니면 그대로 돌려준다 */ if (isupper(m)) w[i] = (char)toupper((unsigned char)w[i]); else if (islower(m)) w[i] = (char)tolower((unsigned char)w[i]); } puts(w); } return 0;}
Craykonmain.cray · 26 lines
n = Console.ReadLine().ToNumber();for (k = 0; k < n; k++){ parts = Console.ReadLine().Trim().Split(" "); p = parts[0]; w = parts[1]; s = ""; for (i = 0; i < w.Length; i++) { m = p[i % p.Length]; // 숫자는 대문자로 바꿔도 그대로라서, 크기 비교로 영어 글자인지부터 가린다 if (m >= "A" && m <= "Z") { s = s + w[i].ToUpper(); } else if (m >= "a" && m <= "z") { s = s + w[i].ToLower(); } else { s = s + w[i]; } } Console.WriteLine(s);}