Strings Using strings as abstract value A string
Strings
Using strings as abstract value • A string is a sequence of characters e. g. “Hello, World!” • Early version of C++ followed the older C language (A string is an array of character, a little support for manipulating strings. ) • New version of C++, introducing a string class. • We can use string as a primitive data type.
Using strings as abstract value • Declaration const string ALPHABET = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
Using strings as abstract value int main() { string name; cout << "Enter your name: "; cin >> name; cout << "Hello, " << name << "!" << endl; return 0; } Enter your name: Wiboon<cr> Hello, Wiboon! Enter your name: Wiboon Promphanich<cr> Hello, Wiboon! The operator >> stops when it hits the first whitespace character. (blank, tab, end-of-line)
#include <iostream> #include <string> using namespace std; int main() { string name; cout << "Enter your name: "; getline(cin, name); cout << "Hello, " << name << "!" << endl; return 0; }
int main() { string name; cout << "Enter your name: "; getline(cin, name); cout << "Hello, " << name << "!" << endl; return 0; } Enter your name: Wiboon<cr> Hello, Wiboon! Enter your name: Wiboon Promphanich<cr> Hello, Wiboon Promphanich!
#include <iostream> #include <string> #include "simpio. h" using namespace std; int main() { string name = get. Line("Enter your full name: "); cout << "Hello, " << name << "!" << endl; return 0; }
String operations • • Class Object Instance Method Free function Sender Receiver Messages
String operations string str = “abc”; int n. Chars = str. length(); str += “abc”; if (str == “quit”) … Relational operators using lexicographic order, defined by ASCII code.
String operations h 0 e 1 l 2 l 3 o 4 , 5 6 w 7 o 8 r 9 l 10 d 11
Selecting character from a string • str[0] no range-checking • str. at(0) range checking • string index and length in string using type size_t, is automatically defined when you include the <string> header file. • str = str 1;
String assignment and extracting • str 2 = str 1; • str. substr(1, 3); • str. substr(7); overwrites content of str 2 return “ell” return “world” string second. Half(string str) { return str. substr(str. length() / 2); }
Searching within a string • str. find(‘o’) return 4 • str. find(‘o’, 5) return 8 • str. find(‘x’) return string: : npos (defined in the string class)
Iterating in a string for (int i = 0; i < str. length(); i++) {. . . body of loop that manipulates str[i]. . . } int count. Spaces(string str) { int n. Spaces = 0; for (int i = 0; i < str. length(); i++) { if (str[i] == ' ') n. Spaces++; } return n. Spaces; }
Iterating in a string for (int i = str. length() - 1; i >= 0; i--) for (int i = 0; i < str. length(); i++)
Iterating in a string bool starts. With(string str, string prefix) { if (str. length() < prefix. length()) return false; for (int i = 0; i < prefix. length(); i++) { if (str[i] != prefix[i]) return false; } return true; }
A string concatenation string str = ""; for (whatever loop header line fits the application) { str += the next substring or character; } string repeat. Char(int n, char ch) { string str = ""; for (int i = 0; i < n; i++) { str += ch; } return str; } cout << repeat. Char(72, '-') << endl;
A string concatenation string reverse(string str) { string rev = ""; for (int i = str. length() - 1; i >= 0; i--) { rev += str[i]; } return rev; }
Methods in <string> library String operators • • • str 1 + str 2 str 1 += str 2 str 1 == str 2 str 1 != str 2 st 1 < str 2 str 1 <= str 2 st 1 > str 2 str 1 >= str 2 str[k] Receiver string unchanged • • • str. length() str. at(k) str. substr(pos, k) str. compare(str 2) str. find(pattern, pos)
Methods in <string> library Modify receiver string Creating C++ and C-style string • str. erase(pos, n) • str. insert(pos, str 2) • str. replace(pos, n, str 2) • string(carray) • string(n, ch) • str. c_str()
Functions in <cctype> library Predicate functions • isalpha(ch) • isupper(ch) • islower(ch) • isdigit(ch) • isxdigit(ch) • isalnum(ch) • ispunct(ch) • isspace(ch) • isprint(ch) Case conversion • toupper(ch) • tolower(ch)
bool is. Digit. String(string str) { if (str. length() == 0) return false; for (int i = 0; i < str. length(); i++) { if (!isdigit(str[i])) return false; } return true; }
bool equals. Ignore. Case(string s 1, string s 2) { if (s 1. length() != s 2. length()) return false; for (int i = 0; i < s 1. length(); i++) { if (tolower(s 1[i]) != tolower(s 2[i])) return false; } return true; }
Modifying the contents of a string void to. Upper. Case. In. Place(string & str) { for (int i = 0; i < str. length(); i++) { str[i] = toupper(str[i]); } }
Modifying the contents of a string to. Upper. Case(string str) { string result = ""; for (int i = 0; i < str. length(); i++) { result += toupper(str[i]); } return result; }
Modifying the contents of a string str = "hello, world"; string str = "hello" + ", " + "world"; (Bug) string str = string("hello") + ", " + "world";
Palindrome bool is. Palindrome(string str) { int n = str. length(); for (int i = 0; i < n / 2; i++) { if (str[i] != str[n - i - 1]) return false; } return true; }
Palindrome bool is. Palindrome(string str) { return str == reverse(str); } The first implementation is more efficient. The second is better for new programmer.
The strlib. h library • • • integer. To. String(n) string. To. Integer(str) real. To. String(d) string. To. Real(str) to. Upper. Case(str) to. Lower. Case(str) equals. Ignore. Case(s 1, s 2) starts. With(str, prefix) ends. With(str, suffix) trim(str)
- Slides: 29