{ }codelper
大文字・小文字文字列の走査P0025

大文字と小文字を入れ替える

一行の文字列が与えられる。
英大文字は小文字に、英小文字は大文字に変え、それ以外の文字(数字・記号・空白)はそのままにして一行で出力せよ。

入力
長さ 1〜200、ASCII の表示可能文字(コード 32〜126)のみ。
空白を含むことがある
出力
変換した文字列一行
例 1 · stdin
Hello, World 2026!
stdout
hELLO, wORLD 2026!
main.py
入力(stdin)

実行すると、これらの行が順に input()・標準入力に渡されます(実行中に入力を求めません)。

$ 準備完了

コードを入力して解くにはパソコンで開いてください。モバイルでは問題と解答を見られます。

ほかの言語では?言語別の解答を見る · 8 言語の解答

一度でやってくれる関数は Python s.swapcase() だけです。
ほかは一文字ずつ回って調べて変えます:Java Character.isUpperCase/toLowerCase、C# char.IsUpper/char.ToLower、C・C++ は <ctype.h> の isupper/tolower(引数は unsigned char で)、JS・TS は c >= "A" && c <= "Z" で調べて toLowerCase()、Craykon は c != c.小文字() なら大文字です。
落とし穴:ASCII で大文字と小文字は 32 違うからと全部の文字に c ^ 32 をすると、数字・記号・空白まで壊れます。
まず英字かどうかを調べましょう。

Pythonmain.py · 2 行
s = input()print(s.swapcase())
JavaScriptmain.js · 8 行
const s = require("fs").readFileSync(0, "utf8").split("\n")[0].replace(/\r$/, "");let out = "";for (const c of s) {  if (c >= "A" && c <= "Z") out += c.toLowerCase();  else if (c >= "a" && c <= "z") out += c.toUpperCase();  else out += c;}console.log(out);
TypeScriptmain.ts · 9 行
import { readFileSync } from "node:fs";const s: string = readFileSync(0, "utf8").split("\n")[0].replace(/\r$/, "");let out = "";for (const c of s) {  if (c >= "A" && c <= "Z") out += c.toLowerCase();  else if (c >= "a" && c <= "z") out += c.toUpperCase();  else out += c;}console.log(out);
JavaMain.java · 16 行
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));        String s = br.readLine();        StringBuilder out = new StringBuilder();        for (char c : s.toCharArray()) {            if (Character.isUpperCase(c)) out.append(Character.toLowerCase(c));            else if (Character.isLowerCase(c)) out.append(Character.toUpperCase(c));            else out.append(c);        }        System.out.println(out);    }}
C#Main.cs · 18 行
using System;using System.Text; class Program{    static void Main()    {        string s = Console.ReadLine();        var sb = new StringBuilder();        foreach (char c in s)        {            if (char.IsUpper(c)) sb.Append(char.ToLower(c));            else if (char.IsLower(c)) sb.Append(char.ToUpper(c));            else sb.Append(c);        }        Console.WriteLine(sb.ToString());    }}
C++main.cpp · 15 行
#include <cctype>#include <iostream>#include <string> int main() {    std::string s;    std::getline(std::cin, s);    if (!s.empty() && s.back() == '\r') s.pop_back();    for (char& c : s) {        unsigned char u = static_cast<unsigned char>(c);        if (std::isupper(u)) c = static_cast<char>(std::tolower(u));        else if (std::islower(u)) c = static_cast<char>(std::toupper(u));    }    std::cout << s << "\n";}
Cmain.c · 16 行
#include <ctype.h>#include <stdio.h>#include <string.h> int main(void) {    char s[256];    if (!fgets(s, sizeof s, stdin)) return 0;    s[strcspn(s, "\r\n")] = '\0';    for (int i = 0; s[i]; i++) {        unsigned char c = (unsigned char)s[i];        if (isupper(c)) s[i] = (char)tolower(c);        else if (islower(c)) s[i] = (char)toupper(c);    }    puts(s);    return 0;}
Craykonmain.cray · 18 行
s = コンソール.行読み();out = "";それぞれ (c の中 s){    もし (c != c.小文字())    {        out = out + c.小文字();    }    でなければ もし (c != c.大文字())    {        out = out + c.大文字();    }    でなければ    {        out = out + c;    }}コンソール.書く(out);