Ch 8. Characters and Strings - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Ch 8. Characters and Strings

Description:

Char can be declared either as signed or unsigned. ... isprint(c)} True if c is a printable character (8 bit only) 4. String Literals ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 19
Provided by: seungs
Category:

less

Transcript and Presenter's Notes

Title: Ch 8. Characters and Strings


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 0while (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 buffer20
  • char text "literal"
  • strcpy(buffer, literal) // copy literal into
    buffer
  • for (int i 0 i lt 5 i)strcat(buffer,
    literal) // append literal to buffer
  • char buffer7
  • 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
    endtext0 'm' // change l to mtext2-- //
    t becomes sstrcpy (text 6, "ble") // replace
    l with ble

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.

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"a2 '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"b2 '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"c2 '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, listltstringgt 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 gt 0) (start lt
    textLen)) // find end of current word int
    stop text.find_first_of(separators,
    start) // check if no ending character if
    ((stop lt 0) (stop gt 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, stop1)

18
  • int main() string text "it was the best of
    times, it was the worst of times."listltstringgt
    wordsstring separators " .,!?"split(text,
    separators, words)string smallest
    words.front()string largest
    words.front()listltstringgtiterator
    currentlistltstringgtiterator stop
    words.end()for (current words.begin()
    current ! stop current) if (current lt
    smallest) smallest current if (largest lt
    current) largest currentcout ltlt
    "smallest word " ltlt smallest ltlt endlcout ltlt
    "largest word " ltlt largest ltlt endlreturn 0
Write a Comment
User Comments (0)
About PowerShow.com