Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2 Ch 8. Characters and Strings Timothy Budd

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

4 3 Character Literals and the 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)

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

6 5 char * text = "A Literal Text"; int vowelCount (char const * p) { // procedure to count vowels in text int 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; }

7 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

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

9 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

10 9 Constant and Mutable Values String literals in Java are immutable, the same is not true in C++. char * text = "literal "; // note space at end text[0] = 'm'; // change l to m text[2]--; // t becomes s strcpy (text + 6, "ble"); // replace l with ble

11 10 Constant and Mutable Values Immutable strings can be formed with the const modifier. But, this modifier can imply multiple meanings, depending on its placement, and the actual meaning may not be intuitive.

12 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 a a = "new literal"; // ok -- can change to new // constant value

13 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 to b = "new literal"; // error -- cannot change pointer itself

14 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 modified c = "new literal"; // error -- cannot change pointer itself

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

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

17 16 Table 8.1 Comparison of string functionality in C++ and Java OperationC++Java Number of Characterslength ()String.length() Readjust lengthresize (newsize, pad)StringBuffer.setLength(newsize) Assigns = s2 Appends +=s2StringBuffer.apend(s2) Catenations + s2String.concat(s2) Character accesss[index]String.charAt(index) Insertioninsert(location, s2)StringBuffer.insert(location, s2) Removalremove(location, length) Replacementreplace(location, length, s2) Substringsubstring(location, length)String.substring(start, end) ComparisonS < s2 Comparisonstrcmp(s.c_str, s2.c_str)String.compareTo(s2) Equality comparisons == s2String.equals(s2) Substring searchfind(text, start)String.indexOf(s2) Character searchfind_first_of(text, start)

18 17 Example - Split a Line into Words void split (const string & text, const string & separators, list & 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)) 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); } }

19 18 int main() { string text = "it was the best of times, it was the worst of times."; list words; string separators = ".,!?:"; split(text, separators, words); string smallest = words.front(); string largest = words.front(); list ::iterator current; list ::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; }


Download ppt "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."

Similar presentations


Ads by Google