Data · 09
Strings
Length, uppercase, reverse, and picking one character.
A string is a row of characters.
The example below reads one word and prints its length, its uppercase form, the word reversed, and its first character.
s = input()print(len(s))print(s.upper())print(s[::-1])print(s[0])const s = require("fs").readFileSync(0, "utf8").trim();console.log(s.length);console.log(s.toUpperCase());console.log([...s].reverse().join(""));console.log(s[0]);import { readFileSync } from "node:fs";const s: string = readFileSync(0, "utf8").trim();console.log(s.length);console.log(s.toUpperCase());console.log([...s].reverse().join(""));console.log(s[0]);import java.util.Scanner; public class Main { public static void main(String[] args) { String s = new Scanner(System.in).nextLine().trim(); System.out.println(s.length()); System.out.println(s.toUpperCase()); System.out.println(new StringBuilder(s).reverse()); System.out.println(s.charAt(0)); }}using System; class Program{ static void Main() { string s = Console.ReadLine().Trim(); Console.WriteLine(s.Length); Console.WriteLine(s.ToUpper()); char[] r = s.ToCharArray(); Array.Reverse(r); Console.WriteLine(new string(r)); Console.WriteLine(s[0]); }}#include <cctype>#include <iostream>#include <string> int main() { std::string s; std::cin >> s; std::cout << s.size() << '\n'; std::string up = s; for (char& c : up) c = std::toupper(static_cast<unsigned char>(c)); std::cout << up << '\n'; std::cout << std::string(s.rbegin(), s.rend()) << '\n'; std::cout << s[0] << '\n';}#include <ctype.h>#include <stdio.h>#include <string.h> int main(void) { char s[101]; scanf("%100s", s); int n = strlen(s); printf("%d\n", n); for (int i = 0; i < n; i++) putchar(toupper((unsigned char)s[i])); putchar('\n'); for (int i = n - 1; i >= 0; i--) putchar(s[i]); putchar('\n'); printf("%c\n", s[0]); return 0;}s = Console.ReadLine();Console.WriteLine(s.Length);Console.WriteLine(s.ToUpper());Console.WriteLine(s.Reverse());Console.WriteLine(s[0]);hello
5 HELLO olleh h
Key points
- Indexes start at 0.
The first character iss[0]and the last is atlength - 1. - Tools such as length, uppercase, reverse, split and trim exist in every language; only the names differ.
- Taking part of a string splits languages in two.
Where you pass an end index,(1, 3)on"abcdef"gives"bc"; where you pass a count, it gives"bcd". - Underneath, a character is a number (its character code):
Ais 65,ais 97.
In C, C++, Java and C#, acharis simply a number, so'A' + 1is66. - Except in C and C++, strings can't be modified.
To change one, build a new string.
How each language writes it
| Python | Length len(s), uppercase s.upper(), reverse s[::-1], split s.split(), trim s.strip(), to number int(s), slice s[1:3] (end index), codes ord("A") → 65, chr(97) → "a" |
|---|---|
| JavaScript | Length s.length, uppercase s.toUpperCase(), reverse [...s].reverse().join("") (strings have no reverse), s.split(" "), s.trim(), slice s.slice(1, 3) (end index), codes s.charCodeAt(0), String.fromCharCode(97) |
| TypeScript | Same as JavaScript |
| Java | Length s.length(), one character s.charAt(i) (not s[i]), uppercase s.toUpperCase(), reverse new StringBuilder(s).reverse(), s.split(" "), s.trim(), slice s.substring(1, 3) (end index) |
| C# | Length s.Length, uppercase s.ToUpper(), reverse with Array.Reverse on the array from ToCharArray(), s.Split(' '), s.Trim(), slice s.Substring(1, 3) (count).Turn a code back into a character with (char) |
| C++ | Length s.size() (or s.length()), uppercase one character at a time with toupper, reverse std::string(s.rbegin(), s.rend()), slice s.substr(1, 3) (count) |
| C | Length strlen(s) — it scans to the closing \0 every time.Uppercase one character at a time with toupper; reverse with a backwards loop |
| Craykon | Length s.Length, uppercase s.ToUpper() (lowercase ToLower), reverse s.Reverse(), s.Split(" "), s.Trim(), to number s.ToNumber(), slice s.Substring(1, 3) (count), codes Str.Code("A"), Str.FromCode(97) |
Common mistakes
Counting Korean text in bytesC · C++
strlen("한글")→ 6, not 2 — it counts bytes, and in UTF-8 one Korean character is 3 bytes (C++ std::string("한글").size() is also 6)
In C and C++, remember that bytes and characters are not the same count.
Python, Java, C#, JavaScript and Craykon say 2.
Changing one character of a stringPython · JavaScript · TypeScript · Craykon
s = "abc"
s[0] = "x"→ Python raises TypeError, JavaScript silently ignores it, TypeScript gives a compile error, and Craykon reports that strings can't be changed one character at a time
Build a new string, e.g. Python "x" + s[1:].
How languages differ
To check whether two strings are equal in Java, use
In C,
To check whether two strings are equal in Java, use
s.equals("yes"), not == (== checks for the same object, so comparing input you read with "yes" gives false).In C,
== compares addresses too, so use strcmp(a, b) == 0; Python, JavaScript, C#, C++ (std::string) and Craykon compare contents with ==.