Download presentation
Presentation is loading. Please wait.
Published byLuke Parsons Modified over 8 years ago
1
Strings
2
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.
3
Using strings as abstract value Declaration const string ALPHABET = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
4
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 Hello, Wiboon! Enter your name: Wiboon Promphanich Hello, Wiboon! The operator >> stops when it hits the first whitespace character. (blank, tab, end-of-line)
5
#include using namespace std; int main() { string name; cout << "Enter your name: "; getline(cin, name); cout << "Hello, " << name << "!" << endl; return 0; }
6
int main() { string name; cout << "Enter your name: "; getline(cin, name); cout << "Hello, " << name << "!" << endl; return 0; } Enter your name: Wiboon Hello, Wiboon! Enter your name: Wiboon Promphanich Hello, Wiboon Promphanich!
7
#include #include "simpio.h" using namespace std; int main() { string name = getLine("Enter your full name: "); cout << "Hello, " << name << "!" << endl; return 0; }
8
String operations Class Object Instance Method Free function Sender Receiver Messages
9
String operations string str = “abc”; int nChars = str.length(); str += “abc”; if (str == “quit”) … Relational operators using lexicographic order, defined by ASCII code.
10
String operations hello,world 01234567891011
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 header file. str = str1;
12
String assignment and extracting str2 = str1;overwrites content of str2 str.substr(1,3);return “ell” str.substr(7);return “world” string secondHalf(string str) { return str.substr(str.length() / 2); }
13
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)
14
Iterating in a string for (int i = 0; i < str.length(); i++) {... body of loop that manipulates str[i]... } int countSpaces(string str) { int nSpaces = 0; for (int i = 0; i < str.length(); i++) { if (str[i] == ' ') nSpaces++; } return nSpaces; }
15
Iterating in a string for (int i = str.length() - 1; i >= 0; i--) for (int i = 0; i < str.length(); i++)
16
Iterating in a string bool startsWith(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; }
17
A string concatenation string str = ""; for (whatever loop header line fits the application) { str += the next substring or character; } string repeatChar(int n, char ch) { string str = ""; for (int i = 0; i < n; i++) { str += ch; } return str; } cout << repeatChar(72, '-') << endl;
18
A string concatenation string reverse(string str) { string rev = ""; for (int i = str.length() - 1; i >= 0; i--) { rev += str[i]; } return rev; }
19
Methods in library String operators str1 + str2 str1 += str2 str1 == str2 str1 != str2 st1 < str2 str1 <= str2 st1 > str2 str1 >= str2 str[k] Receiver string unchanged str.length() str.at(k) str.substr(pos,k) str.compare(str2) str.find(pattern, pos)
20
Methods in library Modify receiver string str.erase(pos, n) str.insert(pos, str2) str.replace(pos, n, str2) Creating C++ and C-style string string(carray) string(n, ch) str.c_str()
21
Functions in 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)
22
bool isDigitString(string str) { if (str.length() == 0) return false; for (int i = 0; i < str.length(); i++) { if (!isdigit(str[i])) return false; } return true; }
23
bool equalsIgnoreCase(string s1, string s2) { if (s1.length() != s2.length()) return false; for (int i = 0; i < s1.length(); i++) { if (tolower(s1[i]) != tolower(s2[i])) return false; } return true; }
24
Modifying the contents of a string void toUpperCaseInPlace(string & str) { for (int i = 0; i < str.length(); i++) { str[i] = toupper(str[i]); }
25
Modifying the contents of a string string toUpperCase(string str) { string result = ""; for (int i = 0; i < str.length(); i++) { result += toupper(str[i]); } return result; }
26
Modifying the contents of a string string str = "hello, world"; string str = "hello" + ", " + "world"; (Bug) string str = string("hello") + ", " + "world";
27
Palindrome bool isPalindrome(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; }
28
Palindrome bool isPalindrome(string str) { return str == reverse(str); } The first implementation is more efficient. The second is better for for new programmer.
29
The strlib.h library integerToString(n) stringToInteger(str) realToString(d) stringToReal(str) toUpperCase(str) toLowerCase(str) equalsIgnoreCase(s1,s2) startsWith(str, prefix) endsWith(str, suffix) trim(str)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.