A line of text is given. Change every uppercase English letter to lowercase and every lowercase English letter to uppercase, leave all other characters (digits, symbols, spaces) unchanged, and print the result on one line.
Input
length 1 to 200, printable ASCII only (codes 32 to 126); spaces may appear
Output
the converted line
Example 1 · stdin
Hello, World 2026!
stdout
hELLO, wORLD 2026!
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
Only Python has a one-call answer: s.swapcase(). Everywhere else you loop over characters, test, and convert: Java Character.isUpperCase/toLowerCase, C# char.IsUpper/char.ToLower, C and C++ isupper/tolower from <ctype.h> (pass an unsigned char), JS/TS test c >= "A" && c <= "Z" then toLowerCase(), and in Craykon c != c.ToLower() means it is uppercase. Trap: since ASCII upper and lower case differ by 32, applying c ^ 32 to every character also wrecks digits, symbols and spaces. Check that it is a letter first.
Pythonmain.py · 2 lines
s = input()print(s.swapcase())
JavaScriptmain.js · 8 lines
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 lines
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 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)); 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 lines
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 lines
#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 lines
#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 lines
s = Console.ReadLine();out = "";foreach (c in s){ if (c != c.ToLower()) { out = out + c.ToLower(); } else if (c != c.ToUpper()) { out = out + c.ToUpper(); } else { out = out + c; }}Console.WriteLine(out);