1 / 18

Ch 8. Characters and Strings

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.

amalie
Download Presentation

Ch 8. Characters and Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ch 8. Characters and Strings Timothy Budd

  2. 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.

  3. 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)

  4. 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.

  5. 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; }

  6. 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

  7. 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.

  8. 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

  9. 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

  10. 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.

  11. 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

  12. 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

  13. 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

  14. 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.

  15. 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) ...

  16. Table 8.1 Comparison of string functionality in C++ and Java

  17. 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); } }

  18. 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; }

More Related