Chapter 10 - Characters, C-Strings, and More About the string Class

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/13

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

14 Terms

1
New cards

Character Testing

  • requires <cctype> header file

<ul><li><p>requires <code>&lt;cctype&gt;</code> header file</p></li></ul><p></p>
2
New cards

Character Case Conversion

  • toupper

    • if char argument is lowercase letter, return the ASCII code for the uppercase equivalent; otherwise, return the argument’s ASCII code unchanged

    • if you want to send the returned value to cout, use a cast expression to display it as a character

      • cout << static_cast<char>toupper(char1);

  • tolower

    • if char argument is uppercase letter, return the ASCII code for its lowercase equivalent; otherwise, return the argument’s ASCII code unchanged

    • use case expression for cout

3
New cards

C-Strings

  • C-String: sequence of characters stored in adjacent memory locations and terminated by NULL character

  • String literal (string constant): sequence of characters enclosed in double quotes “ “

    • ex: “Hi there!“

  • an array of chars can be used to define storage for a string

    • leave room for NULL at the end

    • can enter a value using cin or >>

      • input is whitespace-terminated

      • no check to see if there’s enough space

    • for input containing whitespace, and to control amount of input, use cin.getline()

<ul><li><p><strong>C-String:</strong> sequence of characters stored in adjacent memory locations and terminated by <code>NULL</code> character</p></li><li><p>String literal (string constant): sequence of characters enclosed in double quotes “ “</p><ul><li><p>ex: “Hi there!“</p></li></ul></li><li><p>an array of chars can be used to define storage for a string</p><ul><li><p>leave room for <code>NULL</code> at the end</p></li><li><p>can enter a value using <code>cin</code> or <code>&gt;&gt;</code></p><ul><li><p>input is whitespace-terminated</p></li><li><p>no check to see if there’s enough space</p></li></ul></li><li><p>for input containing whitespace, and to control amount of input, use <code>cin.getline()</code></p></li><li><p></p></li></ul></li></ul><p></p>
4
New cards

Display Contents of Character Array

knowt flashcard image
5
New cards

Library Functions for Working with C-Strings

  • <cstring> header file

  • functions take one or more C-strings as arguments:

    • C-string name

    • pointer to C-string

    • literal string

  • strlen(str)

    • returns length of C-string str

  • strcat(str1, str2)

    • appends str2 to the end of str1

  • strcpy(str1, str2)

    • copies str2 to str1

  • note: strcat and strcpy perform no bounds checking

  • strstr(str1, str2)

    • finds the first occurernce of str2 in str1, returns a match, or NULL if no match

6
New cards

C-String/Numeric Conversion Functions

  • <cstdlib> header file

  • if C-string contains non-digits, results are undefined

    • function may return result up to non-digit

    • function may return 0

  • itoa does no bounds checking

<ul><li><p><code>&lt;cstdlib&gt;</code> header file</p></li><li><p>if C-string contains non-digits, results are undefined</p><ul><li><p>function may return result up to non-digit</p></li><li><p>function may return 0</p></li></ul></li><li><p><code>itoa</code> does no bounds checking</p></li></ul><p></p>
7
New cards

string to Number Conversion

knowt flashcard image
8
New cards

The to_string Function

knowt flashcard image
9
New cards

Writing Your Own C-String Handling Functions

  • can pass arrays or pointers to char arrays

  • can perform bounds checking to ensure enough space for results

  • can anticipate unexpected user input

  • see examples: stringCopy() & nameSlice()

<ul><li><p>can pass arrays or pointers to <code>char</code> arrays</p></li><li><p>can perform bounds checking to ensure enough space for results</p></li><li><p>can anticipate unexpected user input</p></li><li><p>see examples: stringCopy() &amp; nameSlice()</p></li></ul><p></p>
10
New cards

The C++ string Class

  • special data type that supports working with strings

    • #include <string>

  • can define string variables in programs

  • can receive values with assignment operator

  • can be displayed via cout

  • use cin

  • use getline to put a line of input (spaces) into a string

  • can use relational operators directly to compare string objects

    • performed similar to strcmp

11
New cards

Other Definitions of C++ strings

knowt flashcard image
12
New cards

Using auto To Define a string Object

auto str = "Hello World!";

  • defines str as either const char * or const char[12]

  • when using auto, append the s suffix to the string literal

    • auto str = "Hello World!"s;

    • defines str as a string object

13
New cards

string operators

knowt flashcard image
14
New cards

string Member Functions

  • behind many overloaded operators

  • assignment

    • assign, copy, data

  • modification

    • append, clear, erase, insert, replace, swap

  • space management

    • capacity, empty, length, resize, size

  • substrings

    • find, front, back, at, substr

  • comparison

    • compare

  • see Table 10-9 string Class Member Functions