Download presentation
Presentation is loading. Please wait.
Published byAnn Robbins Modified over 8 years ago
1
Characters
2
Character Data char data type – Represents one character – char literals indicated with ' '
3
Char vs String Char – One character – literals use ' ' String – 0+ characters – literals use " "
4
Character Data char is integer data type – 1 byte of space : 256 values – Assumed to represent characters
5
Chars as Integers Integers understood as ASCII codes – http://www.ascii-code.com/ http://www.ascii-code.com/ Can store integers as chars
6
Chars And Math Doing math – char converted to an int:
7
Chars And Math Adding/subtracting and cast back to character shifts char:
8
Letter Number Identify letter of alphabet: 'f' – 'a' + 1 102 – 97 + 1 5 + 1
9
Random Char Generate random char: – Make number 0-25 – Add to 'a'
10
Comparisons Chars compare like other numbers:
11
But Capitals different than lower case: – lower case is "greater"
12
Why is this broken? This prints nothing:
13
Why is this broken? This prints nothing: '3' is char 51 on ASCII chart if( 51 == 3 )
14
Fixed This works:
15
Special Chars Some characters hard to represent – return, tab, ' Escape sequences used to represent them – \ means next char is special: '\t' : tab char '\'' : ' char
16
Escape Codes Common codes:
17
Escape Codes Used in strings and as chars:
18
Case Conversion Convert lower to upper: – Subtract 'a' to find letter number – Add that amount to 'A'
19
Char Functions library has char related functions: – http://www.cplusplus.com/reference/cctype/ http://www.cplusplus.com/reference/cctype/ – all return int is… interpret as boolean to… interpret as char
20
Conversion 2 More readable conversion:
21
Conversion 3 toupper / tolower do their own sanity checks
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.