7: Text and Characters
19 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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.

    True

    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 = ______;.

    <p>'b'</p> Signup and view all the answers

    Match the following operations with their descriptions:

    <p>text.size() = Queries the size of the text std::cin &gt;&gt; next = Reads input character from standard input text.at(0) = Accesses the first character of the text with bounds checking text = 'b' = Writes the character 'b' to the text</p> Signup and view all the answers

    What will happen if multi-byte encoding is used when querying the size of a text?

    <p>Size will not equal text length.</p> Signup and view all the answers

    The expression while (std::cin >> next) continues until the end of input.

    <p>True</p> Signup and view all the answers

    What kind of input does the caesar(s) method expect?

    <p>A shift value (integer)</p> Signup and view all the answers

    What is the purpose of the 'crossed_out' vector in the given code snippet?

    <p>To keep track of numbers that have been processed</p> Signup and view all the answers

    Vectors can only hold data of the same type.

    <p>True</p> Signup and view all the answers

    What technique is used to find all prime numbers up to an integer n?

    <p>Sieve of Eratosthenes</p> Signup and view all the answers

    In the code example, the 'crossed_out' vector is initialized with ______ values.

    <p>false</p> Signup and view all the answers

    Match the following vector features with their descriptions:

    <p>Nested Vectors = A vector that contains other vectors Memory Layout = How elements are stored in contiguous memory locations Iteration = The process of accessing each element in the vector Sieve of Eratosthenes = An algorithm for finding all prime numbers up to a specified integer</p> Signup and view all the answers

    What does the keyword 'const' do in programming?

    <p>Prevents writing to the variable after initialization.</p> Signup and view all the answers

    References can only be initialized with R-values.

    <p>False</p> Signup and view all the answers

    What is the purpose of using references in functions?

    <p>To allow functions to modify the arguments passed to them.</p> Signup and view all the answers

    A variable declared with 'const' can be modified after initialization: true or ______.

    <p>false</p> Signup and view all the answers

    Match the following variable types with their characteristics:

    <p>const = Immutable after initialization int = Integer data type int&amp; = Reference to an integer L-value = Variable with an address in memory</p> Signup and view all the answers

    Which of the following statements regarding references is true?

    <p>References must always be initialized when declared.</p> Signup and view all the answers

    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 of char 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 the for 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 character c by s 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.

    Quiz Team

    Related Documents

    Lecture 7: Vectors PDF

    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.

    More Like This

    Character Quiz
    5 questions

    Character Quiz

    StraightforwardFluorite avatar
    StraightforwardFluorite
    Epic Systems Interview Questions
    14 questions
    Gospel Symbols Representation Quiz
    5 questions
    Content Analysis Quiz
    5 questions

    Content Analysis Quiz

    IlluminatingElegy avatar
    IlluminatingElegy
    Use Quizgecko on...
    Browser
    Browser