C++- and C-Style Strings
- Strings
- Making strings available in your programs
- #include <string>
- Note: strings are also part of <iostream>
- Conceptual View: A string has two parts:
- Contents
- Length
- Note: C++ strings do not have the terminating null
character (e.g., '\0') as do string constants and old-style C strings.
- Declaration:
- Strings are declared as other fundamental variables
- Examples: string s, str, prompt = "Please enter a number";
- Note: strings cannot be initialized by characters (even if they
contain only one element)
- Initialize single character strings as: string one_char = "a";
- Input
- Via an input stream
- By default "white space" terminates the input of a string
- Example
- Via assignment
- Strings can be assigned as other fundamental objects
- Strings can be appended to (using the '+' operator)--called concatenation
- Member Functions (briefly explain difference between member and
non-member functions)
- int size()
- string substr(start, length)
- int find(substr, start)
- void getline(stream, string, term_char)
- [] operator
- Examples
- Examples of various string member functions:
strex.cpp
- Examples using getline(): str.cpp
- Questions