大小写互换
给出一行字符串。
把英文大写字母变成小写、小写字母变成大写,其他字符(数字、符号、空格)保持不变,输出为一行。
- 输入
- 长度 1 到 200,仅 ASCII 可打印字符(编码 32 到 126),可能包含空格
- 输出
- 转换后的一行字符串
Hello, World 2026!
hELLO, wORLD 2026!
给出一行字符串。
把英文大写字母变成小写、小写字母变成大写,其他字符(数字、符号、空格)保持不变,输出为一行。
Hello, World 2026!
hELLO, wORLD 2026!
运行时这些行会依次作为 input()/标准输入传入(运行中不会等待输入)。
$ 就绪
请在电脑上打开以编写和运行代码。在手机上可以查看题目和题解。
一次就能完成的函数只有 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,数字、符号和空格也会被破坏。
要先判断是不是字母。
s = input()print(s.swapcase())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);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);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); }}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()); }}#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";}#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;}s = 控制台.读行();out = "";遍历 (c 在 s){ 如果 (c != c.小写()) { out = out + c.小写(); } 否则 如果 (c != c.大写()) { out = out + c.大写(); } 否则 { out = out + c; }}控制台.输出(out);