Professional Documents
Culture Documents
CPP Characters Strings A3
CPP Characters Strings A3
html
FUNCTION EXAMPLE
int main() {
char * str1 = "hello";
// warning: deprecated conversion from string constant to 'char*'
char * str2 = const_cast<char *>("hello"); // remove the "const"
Take note that you cannot modify the content pointed to by const char *.
1 of 9 22/03/2020, 19:47
C++ Libraries, String and Standard Template Library https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp9_String.html
C-string (null-terminated char array) can be declared as char* or char[]. This is because C treats an array name as a pointer to the first element of the array. Unlike regular arrays,
there is no need to pass the length of C-string into func�on, as the func�on can deduce the length from the termina�ng null character.
FUNCTION EXAMPLE
// Copying
char * strcpy (char * dest, const char * src)
Copy src into dest, return dest
char * strncpy (char * dest, const char * src, size_t n)
Copy at most n characters from src into dest, return dest
// Comparison
int strcmp (const char * cstr1, const char * cstr2)
Compare cstr1 and cstr2. Return 0 if cstr1 is equal to cstr1,
less than zero (usually -1) if cstr1 is less than cstr2,
more than zero (usually 1) if cstr1 is more than cstr2.
int strncmp (const char * cstr1, const char * cstr2, size_t n)
Compare up to n characters.
// Concatenation
char * strcat (char * dest, const char * src)
Append src to dest, return src.
char * strncat (char * dest, const char * src, size_t n)
Append at most n characters from src into dest, return src.
// Searching
char * strchr (char * cstr, int ch);
Return a pointer to the first occurrence of character
char * strrchr (char * cstr, int ch);
Return a pointer to the last occurrence of character
char * strstr (char * cstr1, char * cstr2);
Return a pointer to the first occurrence of cstr2 in cstr1
size_t strspn (const char * cstr, const char * accept)
Return the length (span) of the initial portion of cstr
which consists of only characters in accept
char * strpbrk (char * cstr, const char * accept)
Similar to strspn, but string pointer break returns a pointer
to the first occurrence in cstr of any char in accept
size_t strcspn (const char * cstr, const char * reject)
Complement of strspn. Return the length of the initial
portion of cstr, which does not have char in reject
// Tokenizing
char * strtok (char * cstr, const char * delim)
Tokenize cstr with delim as the delimiters
[TODO] example
FUNCTION EXAMPLE
[TODO] example
FUNCTION EXAMPLE
2 of 9 22/03/2020, 19:47
C++ Libraries, String and Standard Template Library https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp9_String.html
cin.ignore(unsigned int n = 1, int delim = EOF): cin.ignore(256, ' '); // Skip next word
Remove n-1 characters from the input stream, or until delimiter is reached. cin.ignore(1000, '\n'); // Flush input buffer
Example,
string str1("apple");
string str2 = orange;
// '=' is not an assignment, but an implicit call to string's
// constructor str2("orange");
3 of 9 22/03/2020, 19:47
C++ Libraries, String and Standard Template Library https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp9_String.html
Public Functions
Most of the string class func�ons are overloaded to handle string objects, as well as C-string and literals.
// Capacity
size_t size () const; // Return the size of the string
size_t length () const; // same as above
// length() was from earlier version, size() added for compatibility with STL
bool empty () const; // Return true for empty string
void clear (); // Clear to empty string
void resize (size_t n, char pad = '\0'); // resize the string to length n,
// discard trailing characters, or insert pad char
Unlike C-string, which uses a fixed-size array, the string class handles the memory allocated implicitly. In other words, you can append more characters without worrying about
4 of 9 22/03/2020, 19:47
C++ Libraries, String and Standard Template Library https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp9_String.html
exceeding the size. C++ implementa�on may allocate an ini�al block which is larger than the actual string length to allow the content to grow, and allocate more block when the current
block is filled. You can use func�on capacity() to check the current alloca�on, and reserve() to request for a minimum alloca�on.
Example:
1 #include <iostream>
2 #include <string> // C++ string class
3 using namespace std;
4
5 int main() {
6 string strLarge("This is a very very very vary large string");
7 string strSmall("Hi");
8 string strEmpty;
9
10 cout << "size=" << strLarge.size() << " capacity=" << strLarge.capacity() << endl;
11 cout << "size=" << strSmall.size() << " capacity=" << strSmall.capacity() << endl;
12 cout << "size=" << strEmpty.size() << " capacity=" << strEmpty.capacity() << endl;
13
14 cout << "string::npos=" << string::npos << endl;
15 cout << "max_size=" << strEmpty.max_size() << endl;
16
17 strSmall.reserve(100);
18 cout << "size=" << strSmall.size() << " capacity=" << strSmall.capacity() << endl;
19
20 strLarge.resize(10);
21 cout << strLarge << endl;
22 cout << "size=" << strLarge.size() << " capacity=" << strLarge.capacity() << endl;
23 strSmall.resize(10, '-');
24 cout << strSmall << endl;
25 cout << "size=" << strSmall.size() << " capacity=" << strSmall.capacity() << endl;
26
27 strLarge.clear();
28 cout << "size=" << strLarge.size() << " capacity=" << strLarge.capacity() << endl;
29 strLarge.shrink_to_fit(); // C++11
30 cout << "size=" << strLarge.size() << " capacity=" << strLarge.capacity() << endl;
31 }
size=42 capacity=42
size=2 capacity=2
size=0 capacity=0
string::npos=4294967295
max_size=1073741820
size=2 capacity=100
This is a
size=10 capacity=42
Hi--------
size=10 capacity=100
size=0 capacity=42
size=0 capacity=0
// C-string
c_str;
Some func�ons, such as ofstream's open() which opens a file, accept only C-string. You can use c_str to get a C-string from an string object.
// Element Access
char & operator[] (size_t pos); // Return char at pos, no index range check
char & at (size_t pos); // Return char at pos, with index range check
char & front (); // Return first char
char & back (); // Return last char
string substr (size_t pos = 0, size_t len = npos) const; // Return a substring
// Modifying
append
insert
assign
erase
replace
swap
push_back
pop_back
compare
// Searching
size_t find (const string & str, size_t pos = 0) const;
size_t find (const char * cstr, size_t pos = 0) const;
size_t find (const char * cstr, size_t pos, size_t n) const;
size_t find (char c, size_t pos = 0) const;
// Find the first occurrence of the string or char, starting from pos
// Return the index or string::npos if not found
rfind: last occurrence
find_first_of: first occurrence of ANY of the character in the string
find_last_of: last occurrence of ANY of the character in the string
find_first_not_of: first occurrence of NOT ANY of the character in the string
find_last_not_of: last occurrence of NOT ANY of the character in the string
5 of 9 22/03/2020, 19:47
C++ Libraries, String and Standard Template Library https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp9_String.html
4 #include <iostream>
5 #include <string>
6 #include <cstdlib>
7 #include <ctime>
8 #include <cctype>
9 using namespace std;
10
11 const int NUM_WORDS = 18;
12 const string WORD_LIST[NUM_WORDS] = {
13 "apple", "orange", "banana", "watermelon", "pear",
14 "pineapple", "papaya", "mango", "grape", "strawberry",
15 "lemon", "peach", "cherry", "apricot", "coconut",
16 "honeydew", "apricot", "blueberry"};
17
18 int main() {
19 // Seed the pseudo-random number generator
20 srand(time(0));
21
22 bool over = false; // gameover
23 do {
24 string target = WORD_LIST[rand() % NUM_WORDS]; // choose a word between 0 to NUM_WORDS-1
25 int target_length = target.length();
26 string attempt(target_length, '-'); // init to all dashes
27 string badChars; // contains bad chars used
28 int trial = 1; // number of trials
29
30 cout << "Guess the secret word" << endl;
31
32 while (attempt != target) {
33 char letter;
34 cout << "Your guess so far: " << attempt << endl;
35 cout << "Trial " << trial << ": Guess a letter: ";
36 cin >> letter;
37
38 // Check if the letter has been used
39 if (badChars.find(letter) != string::npos
40 || attempt.find(letter) != string::npos) {
41 cout << "You already use this letter. Try again.\n";
42 continue;
43 }
44
45 // Check for good or bad letter
46 int pos = target.find(letter);
47 if (pos == string::npos) {
48 cout << "Oh, bad guess!\n";
49 badChars += letter; // add to badChars string
50 } else {
51 cout << "Good guess!\n";
52 attempt[pos] = letter;
53 // Check if this letter appears again later
54 do {
55 pos = target.find(letter, pos + 1);
56 if (pos != string::npos) attempt[pos] = letter;
57 } while (pos != string::npos);
58 }
59
60 ++trial;
61 }
62
63 cout << "You got it in " << trial << " trials! The secret word is \""
64 << target << "\"" << endl;
65
66 char playAgain;
67 cout << "Another game? <y/n> ";
68 cin >> playAgain;
69 if (playAgain != 'y' && playAgain != 'Y') over = true;
70 } while (!over);
71
72 cout << "Bye\n";
73 return 0;
74 }
#include <algorithm>
#include <string>
#include <cctype>
6 of 9 22/03/2020, 19:47
C++ Libraries, String and Standard Template Library https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp9_String.html
C-string has three input methods: stream extrac�on operator (>>), getline() and get(). All 3 func�ons belong to the istream class.
// Example usage:
char str[80];
cin >> str;
It extracts characters from the input stream and store into str, un�l either a whitespace (blank, tab, newline) is encountered or width-1 characters is read. A termina�ng null
character is automa�cally inserted. The default width is 0, indica�ng unlimited. You can set the width via cin >> setw(n) (in header <iomanip>) or cin.width(n).
setw() is non-s�cky and is only applicable to next input opera�on. It is reset to 0. The trailing whitespace is le� in the input stream. The leading white spaces are ignored.
For example,
We set the width to the SIZE (=5) of the char[]. cin << reads up to 4 characters or whitespace.
If you enter "12\n", "12" is read into str, and "\n" remains in the input buffer.
If you enter "12345\n", "1234" is read into str, and "5\n" remains in the input buffer.
If you enter "12 345\n", "12" is read into str, and " 345\n" remains in the input buffer.
The next cin >> discards the leading whitespaces and starts with the first non-whitespace character.
If width is not set properly and if the input (including the termina�ng null character) exceeds the size of char array, memory will be corrupted.
You may need to flush the input buffer (see below).
istream::getline()
The getline() func�on of the istream class has the following prototypes:
// Example usage
char str[80];
cin.getline(str, 80); // Read up to 79 chars or '\n'; discarding '\n'
cin.getline(str, 80, ':'); // Read up to 79 chars or ':'; discarding ':'
getline() extracts characters from the input stream and stores in str, un�l either it reaches the delimiter character (default of '\n'), or n-1 character is wri�en to str. A
termina�ng null character is automa�cally inserted. The delimiter, if found, is extracted and discarded from input stream. You can use cin.gcount() to get the number of characters
extracted (including delim). getline() reads all whitespace (including leading whitespaces which is ignore by cin >>).
If n-1 character is read and the next character is not delimit, then the failbit (of the istream) is set. You can check the failbit via bool func�on cin.fail(). You need to
invoke cin.clear() to clear the error bit. Otherwise, all subsequent getline() will fail and return gcount() of 0.
For example,
if (cin.fail()) {
cout << "failbit is set!" << endl;
cin.clear(); // Clear all error bits. Otherwise, subsequent getline() fails
}
Again, we set n to the SIZE (=5) of char[]. getline() reads up to 4 characters or '\n'.
If you enter "xxxx\n", where xxxx may include whitespaces, "xxxx" is read into str, and "\n" discarded from input buffer.
If you enter "xxxxyyy\n", "xxxx" is read into str, "yyy\n" remains in input buffer. failbit is set. You need to clear the error bits before issuing another getline() call.
istream::get()
The get() func�on of the istream class has the following prototypes for C-string:
// C-string
istream & get (char * str, int n);
istream & get (char * str, int n, char delim);
get(str, n, delim) is similar to getline(str, n, delim), except that the delim character is not extracted and remains in the input stream.
Single-character Input
The get() member func�on is more o�en used to read a single character, unforma�ed, including whitespaces and newlines. It has the following two versions:
// Single character
istream & get (char & c); // Read next character into the char reference and
// return the istream for concatenated operations
// It returns null pointer (converted to false) at end-of-file
7 of 9 22/03/2020, 19:47
C++ Libraries, String and Standard Template Library https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp9_String.html
// Examples
char ch;
while (cin.get(ch) && ch != '\') { ...... } // read all characters until newline
// You cannot use cin >> ch, because it ignores the whitespace
while (cin.get(ch)) { ...... } // read until end-of-file
int ch;
while (ch = cin.get() != EOF) { ....... } // read until end-of-file
When the end-of-file is encountered, get(char &) returns a null pointer, which is converted to false. On the other hand, get(void) return a special value EOF (defined in
<iostream>). get(char &) has no room to represent any special symbol such as EOF!
In brief, there are 3 methods for reading single character, cin >> char, get(char &) and get(void). cin >> char skips whitespaces, get(char &) reads a character by
reference, while get(void) reads a character and return an int. get(void) is closely resembled the C's getchar(void) func�on (in stdio.h) and can be used to convert C
programs into C++.
Others
istream::peek(), istream::unget(), etc.
// Example usage
string str;
cin >> str;
// Compared with C-string, no need to set the width
It extracts a string (word) from the input stream, delimited by whitespace (blank, tab or newline), and stores in str, overriding its previous value. The size of str is determined by the
length of input.
string::getline()
The string class also provides a friend func�on getline():
// Example usage
string str;
getline(cin, str); // default delimiter of '\n'
getline(cin, str, ':');
// Compared with C-string:
// 1. No need to specify a limit
// 2. NOT cin.getline()
// C-string getline() is a member function of istream class
// string class' getline() is a non-member friend function
It extracts characters from the input stream into str un�l the delimiter character is found (default of newline '\n') or end-of-file is reached. The delimiter is extracted and discard
from the input buffer, i.e., the next opera�on will begin a�er the delimiter. The size of str is determined by the length of input.
Notes
The maximum allowable size for a string object is defined by the constant string::npos, which is typically the maximum value of unsigned int.
If getline() is used to read from a file, and end-of-file is encountered, the eofbit of the input stream will be set. That is, fail() and eof() of the input stream will return
true.
If the maximum allowable size of string is reached, or memory exhausted, the failbit of the input stream will be set. That is, fail() of the input stream will return true.
An input stream maintains its status in these bits: goodbit (good() func�on) indicates all is fine; failbit (fail() func�on) indicates input error; badbit (bad()
func�on) indicates recognized failure such as hardware failure; eofbit (eof() func�on) indicates end-of-file detected.
8 of 9 22/03/2020, 19:47
C++ Libraries, String and Standard Template Library https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp9_String.html
Feedback, comments, corrections, and errata can be sent to Chua Hock-Chuan (ehchua@ntu.edu.sg) | HOME
9 of 9 22/03/2020, 19:47