Podcast
Questions and Answers
What does the expression if (text1 == text2)
evaluate to?
What does the expression if (text1 == text2)
evaluate to?
- True if text1 is character-wise equal to text2 (correct)
- False if text2 is empty
- True if text1 is longer than text2
- False if text1 contains multi-byte characters
The method text.at(0)
checks the index bounds of the text.
The method text.at(0)
checks the index bounds of the text.
True (A)
What is the purpose of the variable length
in the code snippet?
What is the purpose of the variable length
in the code snippet?
To store the size of the text
The method used to write a single character to the text is text = ______;
.
The method used to write a single character to the text is text = ______;
.
Match the following operations with their descriptions:
Match the following operations with their descriptions:
What will happen if multi-byte encoding is used when querying the size of a text?
What will happen if multi-byte encoding is used when querying the size of a text?
The expression while (std::cin >> next)
continues until the end of input.
The expression while (std::cin >> next)
continues until the end of input.
What kind of input does the caesar(s)
method expect?
What kind of input does the caesar(s)
method expect?
What is the purpose of the 'crossed_out' vector in the given code snippet?
What is the purpose of the 'crossed_out' vector in the given code snippet?
Vectors can only hold data of the same type.
Vectors can only hold data of the same type.
What technique is used to find all prime numbers up to an integer n?
What technique is used to find all prime numbers up to an integer n?
In the code example, the 'crossed_out' vector is initialized with ______ values.
In the code example, the 'crossed_out' vector is initialized with ______ values.
Match the following vector features with their descriptions:
Match the following vector features with their descriptions:
What does the keyword 'const' do in programming?
What does the keyword 'const' do in programming?
References can only be initialized with R-values.
References can only be initialized with R-values.
What is the purpose of using references in functions?
What is the purpose of using references in functions?
A variable declared with 'const' can be modified after initialization: true or ______.
A variable declared with 'const' can be modified after initialization: true or ______.
Match the following variable types with their characteristics:
Match the following variable types with their characteristics:
Which of the following statements regarding references is true?
Which of the following statements regarding references is true?
Flashcards
text
text
A sequence of characters, typically used to represent text.
character
character
A single character in a text, representing a letter, number, symbol, or whitespace.
accessing text characters
accessing text characters
Accessing characters in a text, using an index starting from 0 for the first character.
comparing texts
comparing texts
Signup and view all the flashcards
text size
text size
Signup and view all the flashcards
reading a character
reading a character
Signup and view all the flashcards
writing a character
writing a character
Signup and view all the flashcards
iterating over characters
iterating over characters
Signup and view all the flashcards
Vector
Vector
Signup and view all the flashcards
Nested Vector
Nested Vector
Signup and view all the flashcards
Vector Type
Vector Type
Signup and view all the flashcards
Vector Iteration
Vector Iteration
Signup and view all the flashcards
Vector Memory Layout
Vector Memory Layout
Signup and view all the flashcards
const
const
Signup and view all the flashcards
Reference
Reference
Signup and view all the flashcards
References and initialization
References and initialization
Signup and view all the flashcards
References in functions
References in functions
Signup and view all the flashcards
References and data types
References and data types
Signup and view all the flashcards
Const reference
Const reference
Signup and view all the flashcards
Study Notes
Introduction to Computer Science
- Course numbers: 252-0032, 252-0047, 252-0058
- Authors: Manuela Fischer and Felix Friedrich
- Department: Computer Science, ETH Zurich
- Semester: Fall 2024
Characters and Texts
- Characters: Basic building blocks of text, represented by the
char
data type. Printable characters (e.g., 'a') and control characters (e.g., '\n') - Strings: Sequences of characters, represented by the
std::string
type. Act like vectors ofchar
elements. - ASCII: Standard code for representing characters. Converts characters to numerical values. Letters and digits have specific numerical assignments. Supports only 128 (or 256) characters which is too few for representing a lot of characters from different scripts today (like Chinese, Cyrillic, Greek, Hebrew, Latin, etc.)
- UTF-8: Variable-width encoding used for representing wider character sets (internationalization). Handles multiple scripts and more than 137,000 characters. More common characters use fewer bytes, while uncommon ones use more bytes. UTF-8 is a popular variable width encoding that encodes characters using 1-6 bytes, providing more space to add more characters.
Example: Caesar Cipher
- Caesar Cipher: Simple substitution cipher that shifts each character by a fixed number (e.g. positive or negative integer) in the alphabet. Wraps around at the end of the alphabet.
Text Manipulation
- Declaration and Initialization: Methods for defining
std::string
variables and assigning values like literals or specific characters. - Comparison: Comparing strings using equality operator (
==
). - Character Access: Accessing individual characters in strings by their index or using the
at()
function (which checks bounds for safety). This check is important to avoid accessing outside of valid array indices. - Iteration: Traversing through characters in strings using loops (iterating over individual characters).
- Concatenation: Joining strings together using the
+=
operator (e.g.text += "!";
). - Size: Determining the length of a string using the
.size()
method. This is also helpful for thefor
loop when correctly iterating through text characters.
Additional Information
- Input Handling: The
std::cin >> std::noskipws;
directive is used to read characters including whitespace from user input. - Modulo Function: A function
mod(dividend, divisor)
is defined for calculating the mathematical modulo remainder (correct for negative numbers as well). This helps ensure the correct mathematical operations. - Character Shifting: A function
shift(c, s)
is used to cyclically shift a printable characterc
bys
positions in the ASCII character set. This is useful in the Caesar Cipher to shift characters. Characters outside the printable range [32,126] are not shifted. std::noskipws
: Used in input to not skip whitespace characters.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
This quiz explores the fundamental concepts of characters and strings in computer science. It covers character representation, ASCII encoding, UTF-8, and includes an example of a Caesar cipher. Perfect for students looking to strengthen their understanding of text processing.