180 likes | 326 Views
Ch 8. Characters and Strings. Timothy Budd. Characters and Literals Strings. Char in C++ is normally an 8-bit quantity, whereas in Java it is a 16-bit value. Char can be declared either as signed or unsigned . wchar_t represents a wide character, 16-bit character.
E N D
Ch 8. Characters and Strings Timothy Budd
Characters and Literals Strings • Char in C++ is normally an 8-bit quantity, whereas in Java it is a 16-bit value. • Char can be declared either as signed or unsigned. • wchar_t represents a wide character, 16-bit character.
Character Literals andthe cctype Library isalpha(c) True if c is alphabetic isupper(c) True if c is upper case islower(c) True if c is lower case isdigit(c) True if c is decimal digit char isxdigit(c) True if c is hexidecimal digit isalnum(c) True if c is alphabetic or numeric isspace(c) True if c is whitespace (space, tab or newline) isprint(c)} True if c is a printable character (8 bit only)
String Literals • A literal string value has type array of character in C++, whereas it has type string in Java. • Always remember the null character at the end of a string literal.
char * text = "A Literal Text"; int vowelCount (char const * p) {// procedure to count vowels in textint sum = 0;while (1) { // will break out of loop in switch statement switch (*p++) { // case '\0': return sum; case 'a': case 'e': case 'i': case 'o': case 'u': sum++; break; }}return sum; }
The cstring Library • defined by header file string.h, should not be confused with the header file string. strcpy(dest, src) Copy characters from source to destination strncpy(dest, src, n) Copy exactly n characters strcat(dest, src) Append characters from source onto destination strncat(dest, src, n) Append only n characters strcmp(s1, s2) Compare strings s1 and s2 strncmp(s1, s2, n) Compare first n characters in s1 and s2 strlen(s) Count number of characters in s
The cstring Library • Comparison between pointer values is determined by the relative placement in memory of the locations being pointed to. • To determine a lexicographic comparison of two character pointer values, an explicit comparison function must be used. • If the first string is lexicographically smaller, return less than zero, if equal, returns zero, otherwise larger than zero.
int strcmp (const unsigned char * p, const unsigned char * q) { while (*p && (*p == *q)) { p++; q++; } return *p - *q; } char buffer[20]; char * text = "literal"; strcpy(buffer, literal); // copy literal into buffer for (int i = 0; i < 5; i++)strcat(buffer, literal); // append literal to buffer char buffer[7]; strcpy (buffer, "literal"); // error! - copies eight values, not seven
Constant and Mutable Values • String literals in Java are immutable, the same is not true in C++. char * text = "literal "; // note space at endtext[0] = 'm'; // change l to mtext[2]--; // t becomes sstrcpy (text + 6, "ble"); // replace l with ble
Constant and Mutable Values • Immutable strings can be formed with the constmodifier. • But, this modifier can imply multiple meanings, depending on its placement, and the actual meaning may not be intuitive.
Constant and Mutable Values • Placing the const at the front: cannot be modified, but it can be reassigned to a new constant string: const char * a = "literal";a[2] = 'z'; // error -- cannot modify aa = "new literal"; // ok -- can change to new // constant value
Constant and Mutable Values • Placing the const in the middle: the pointer cannot be changed, but the value it references can. char * const b = "literal";b[2] = 'z'; // ok -- can change what it points tob = "new literal"; // error -- cannot change pointer itself
Constant and Mutable Values • A value cannot be changed and be reassigned, can only be formed by using two occurrences of the const modifier: const char * const c = "literal";c[2] = 'z'; // error -- cannot be modifiedc = "new literal"; // error -- cannot change pointer itself
The string Data Type • The string data type is a recent addition to C++ and is still not widely used. • The string abstraction is similar to combination of the String and StringBuffer data types in Java.
string a; string b = "initial text"; string c("more text"); string d(b); // copy of b a = "some text"; // assignment to a • Subscript index values are not checked for validity. // see if string b is prefix to a if (a.substr(0, b.length()) == b) ...
Table 8.1 Comparison of string functionality in C++ and Java
Example - Split a Line into Words void split (const string & text, const string & separators, list<string> & words) // split a string into a list of words text and separators are input, // list of words is output { int textLen = text.length(); // find first non-separator character int start = text.find_first_not_of(separators, 0); // loop as long as we have a non-separator character while ((start >= 0) && (start < textLen)) { // find end of current word int stop = text.find_first_of(separators, start); // check if no ending character if ((stop < 0) || (stop > textLen)) stop = textLen; // add word to list of words words.push_back (text.substr(start, stop - start)); // find start of next word start = text.find_first_not_of (separators, stop+1); } }
int main() {string text = "it was the best of times, it was the worst of times.";list<string> words;string separators = " .,!?:";split(text, separators, words);string smallest = words.front();string largest = words.front();list<string>::iterator current;list<string>::iterator stop = words.end();for (current = words.begin(); current != stop; ++current) { if (*current < smallest) smallest = *current; if (largest < *current) largest = *current;}cout << "smallest word " << smallest << endl;cout << "largest word " << largest << endl;return 0; }