Lecture 8: Characters and Texts PDF
Document Details
Uploaded by FervidDune
ETH Zurich
Tags
Summary
This document is a lecture handout on characters and texts in C++. It covers topics such as ASCII, UTF-8, Caesar code, and the use of strings in C++. The language used is C++ and the document type is a lecture handout.
Full Transcript
13. Characters and Texts Characters, ASCII, UTF-8, Caesar Code, Text as Strings, String Operations 365 Characters and Texts We have seen texts before: std::cout > next) { shall beinput...
13. Characters and Texts Characters, ASCII, UTF-8, Caesar Code, Text as Strings, String Operations 365 Characters and Texts We have seen texts before: std::cout > next) { shall beinput ignored only not if the is empty std::cout > s; // Shift input by s caesar(s); Encode: shift by −n (here: -3) return 0; } 375 Texts Text “to be or not to be” could be represented as vector Texts are ubiquitous, however, and thus have their own typ in the standard library: std::string Requires #include 376 Using std::string Declaration, and initialisation with a literal: std::string text = "Essen ist fertig!" Initialise with variable length: std::string text = std::string(n, ’a’) text is filled with n ’a’s Comparing texts: if (text1 == text2)... true if character-wise equal 377 Using std::string Querying size: int length = text.size(); Size not equal to text length if multi-byte encoding is used, e.g. UTF-8 Reading single characters: if (text == 'a')... // or text.at(0) text does not check index bounds, whereas text.at(0) does Writing single characters: text = 'b'; // or text.at(0) Iterating over characters for (int i = 0; i < text.size(); ++i) std::cout