1/13
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Character Testing
requires <cctype> header file

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
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()

Display Contents of Character Array

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
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

string to Number Conversion

The to_string Function

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()

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
Other Definitions of C++ strings

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
string operators

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