C for Java Programmers - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

C for Java Programmers

Description:

Char in C is normally an 8-bit quantity, whereas in Java it is a 16-bit value. ... classic (and cryptic) example of // character pointer manipulation ... – PowerPoint PPT presentation

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

less

Transcript and Presenter's Notes

Title: C for Java Programmers


1
C for Java Programmers
  • Chapter 8
  • Characters and Strings

2
Characters and Literal 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.
  • Char a abc
  • // a has 4 elements, not 3

5
String Literals
  • 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 strcmp function
  • 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
Definition of strcmp
  • int strcmp
  • (const unsigned char p, const unsigned char
    q) while (p (p q)) p q
    return p - q
  • // classic (and cryptic) example of
  • // character pointer manipulation

9
strcpy, strcat - copy strings
  • 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

10
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
  • // this sort of thing makes a literal miserable

11
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
Const strings
  • 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

13
A different type of const
  • 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

14
Really constant
  • 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

15
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
Creating strings
  • 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.

17
Table 8.1 Comparison of string functionality in
C and Java
18
Test your understanding
  • How many bits in a C char?
  • What functions are found in cctype?
  • What is the type of a string literal?
  • What functions are found in cstring?

19
Test your understanding
  • Why is the following probably in error
  • char x here now
  • char y there now
  • if (x lt y)

20
Test your understanding
  • Why is the following probably in error
  • char buffer10
  • strcpy(buffer, now is the winter of our
    discontent)
Write a Comment
User Comments (0)
About PowerShow.com