CS1313 Characters and Strings Lesson 1

CS1313 Characters and Strings Lesson 1

Characters Lesson Outline 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. Characters Lesson Outline Numeric Encoding of Non-numeric Data #1 Numeric Encoding of Non-numeric Data #2 Representing Characters How Characters Are Represented #1 How Characters Are Represented #2 Representing Digits Representing Punctuation ASCII ASCII Table #1 ASCII Table #2 ASCII Table #3 ASCII Table #4 ASCII Confirmation Program #1 ASCII Confirmation Program #2 ASCII Confirmation Program #3 ASCII Confirmation Program #4 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. A char is an int #1 A char is an int #2 Declaring char Scalar Variables #1 Declaring char Scalar Variables #2 char Like int Example char Scalar Literal Constants char Scalar Literal Constant Example Using char Scalar Variables Using char Scalar Variables Example char Arrays #1

char Arrays #2 Character Array Example #1 Character Array Example #2 Characters Lesson CS1313 Fall 2019 1 Numeric Encoding of Non-numeric Data #1 In Programming Project #4, we encoded (represented) entree item choices using integer values: 1. steak 2. chicken 3. vegetarian lasagna If we wanted to add other entrees, for example: 4. lamb 5. gumbo Characters Lesson CS1313 Fall 2019 2 Numeric Encoding of Non-numeric Data #2 1. steak 2. chicken 3. vegetarian lasagna 4. lamb 5. gumbo ... The numbers in these cases have no standard meaning with respect to the items that they encode; theyve been chosen essentially at random. So, we see that we can encode qualitative (non-numeric) values with quantitative (numeric) values, using arbitrary but distinct numeric values to encode a set of qualities. Characters Lesson CS1313 Fall 2019 3 Representing Characters Whats the most important set of non-numeric values in computing? Its the one that allows the computer to communicate with us in a way that makes sense to actual real live human beings: natural language. The most efficient way for computers to communicate in a natural language is by writing. Writing is based on characters. Characters are non-numeric. So, we want a way to encode characters numerically. Characters Lesson CS1313 Fall 2019 4 How Characters Are Represented #1

Heres a code you might have used to play secret code games when you were a kid: 'A' = 1, 'B' = 2, 'C' = 3, 'D' = 4, . . ., 'Z' = 26 Now that youve grown up and taken CS1313, you realize that the numbers that you choose can be arbitrary, as long as theyre fixed and distinct. So you could just as easily choose: 'A' = 65, 'B' = 66, 'C' = 67, 'D' = 68, . . ., 'Z' = 90 This is a perfectly reasonable encoding, if the only characters that you care about are upper case letters. What about lower case? Characters Lesson CS1313 Fall 2019 5 How Characters Are Represented #2 'A' = 65, 'B' = 66, 'C' = 67, 'D' = 68, . . ., 'Z' = 90 What about lower case? Well, you could add, for example: 'a' = 97, 'b' = 98, 'c' = 99, 'd' = 100, . . ., 'z' = 122 Are these the only characters that you need? Characters Lesson CS1313 Fall 2019 6 Representing Digits Another kind of very important character is a digit. Heres a possible encoding of the decimal digits: '0' = 48, '1' = 49, '2' = 50, '3' = 51, . . ., '9' = 57 Notice that theres an important distinction between the character to be represented, which happens to be a digit, and the numeric encoding, whose value doesnt have to have anything to do with the value of the digit being encoded. Characters Lesson CS1313 Fall 2019 7 Representing Punctuation In addition to the upper case letters, the lower case letters and the digits, we also need to encode special characters such as punctuation. This is starting to get pretty complicated, so maybe itd help to have a standardized system. Characters Lesson CS1313 Fall 2019 8 ASCII The American Standard Code for Information Interchange (ASCII)* is a standardized system for encoding characters numerically.

It has several categories of characters: letters: digits ('0' = 48 through '9' = 57); punctuation * upper case ('A' = 65 through 'Z' = 90); lower case ('a' = 97 through 'z' = 122); space = 32 through slash = 47; colon = 58 through at sign = 64; open square bracket = 91 through backquote = 96; open curly brace = 123 through tilde = 126; control characters, encoded as 0 through 31; also DEL (encoded as 127). http://www.asciitable.com/ Characters Lesson CS1313 Fall 2019 9 ASCII Table #1 Code Char 0 NUL 1 SOH 2 Kbd Name Code Char Kbd

Name Null 16 DLE Ctrl-P Data Line Escape Ctrl-A Start of Heading 17 DC1 Ctrl-Q Device Control 1 STX Ctrl-B Start of Text 18 DC2 Ctrl-R Device Control 2 3 ETX Ctrl-C End of Text 19 DC3 Ctrl-S Device Control 3 4 EOT Ctrl-D

End of Transmission 20 DC4 Ctrl-T Device Control 4 5 ENQ Ctrl-E Enquiry 21 NAK Ctrl-U Negative Acknowledge 6 ACK Ctrl-F Acknowledge 22 SYN Ctrl-V Synchronous File 7 BEL Ctrl-G Ring Bell 23 ETB Ctrl-W End Transmission Block 8 BS

Ctrl-H Backspace 24 CAN Ctrl-X Cancel 9 HT Ctrl-I Horizontal Tab 25 EM Ctrl-Y End of Medium 10 LF Ctrl-J Line Feed 26 SUB Ctrl-Z Substitute 11 VT Ctrl-K Vertical Tab 27 ESC Ctrl-Shift-K Escape

12 FF Ctrl-L Form Feed 28 FS Ctrl-Shift-L File Separator 13 CR Ctrl-M Carriage Return 29 GS Ctrl-Shift-M Group Separator 14 SO Ctrl-N Shift Out 30 RS Ctrl-Shift-N Record Separator 15 SI Ctrl-O Shift In 31 US Ctrl-Shift-O

Unit Separator Characters Lesson CS1313 Fall 2019 10 ASCII Table #2 Code Char 32 Name Code Char Blank space 48 0 Name 33 ! Exclamation point 49 1 34 " Double quote 50 2 35 # Pound 51 3 36

$ Dollar sign 52 4 37 % Percent 53 5 38 & Ampersand 54 6 39 ' Single quote 55 7 40 ( Open parenthesis 56 8 41 ) Close parenthesis 57 9 42 *

Asterisk 58 : Colon 43 + Plus 59 ; Semicolon 44 , Comma 60 < Less than 45 - Hyphen 61 = Equals 46 . Period 62 > Greater than 47 /

Slash 63 ? Question mark Characters Lesson CS1313 Fall 2019 11 ASCII Table #3 Code Char Name Code Char Name 64 @ At 80 P 65 A 81 Q 66 B 82 R 67 C 83 S 68

D 84 T 69 E 85 U 70 F 86 V 71 G 87 W 72 H 88 X 73 I 89 Y 74 J 90 Z 75 K 91

[ Open square bracket 76 L 92 \ Backslash 77 M 93 ] Close square bracket 78 N 94 ^ Caret 79 O 95 _ Underscore Characters Lesson CS1313 Fall 2019 12 ASCII Table #4 Code Char Name Code Char Name

96 ` Accent grave 112 p 97 a 113 q 98 b 114 r 99 c 115 s 100 d 116 t 101 e 117 u 102 f 118 v 103

g 119 w 104 h 120 x 105 i 121 y 106 j 122 z 107 k 123 { Open curly brace 108 l 124 | Vertical bar 109 m 125 } Close curly brace 110

n 126 ~ Tilde 111 o 127 DEL Delete Characters Lesson CS1313 Fall 2019 13 ASCII Confirmation Program #1 #include int main () { /* main */ const int first_printable_character_code = 32; const int last_printable_character_code = 126; const int program_success_code = 0; int index; for (index = first_printable_character_code; index <= last_printable_character_code; index++) { printf("ASCII Code #%3d is: %c\n", index, index); } /* for index */ return program_success_code; } /* main */ Characters Lesson CS1313 Fall 2019 14 ASCII Confirmation Program #2 % gcc -o asciitest asciitest.c % asciitest ASCII Code # 32 is: ASCII Code # 33 is: ! ASCII Code # 34 is: " ASCII Code # 35 is: # ASCII Code # 36 is: $ ASCII Code # 37 is: % ASCII Code # 38 is: & ASCII Code # 39 is: ' ASCII Code # 40 is: ( ASCII Code # 41 is: )

ASCII Code # 42 is: * ASCII Code # 43 is: + ASCII Code # 44 is: , ASCII Code # 45 is: ASCII Code # 46 is: . ASCII Code # 47 is: / ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII Characters Lesson CS1313 Fall 2019 Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code # # # # # # # # # # # # # # # # 48

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 is: is: is: is: is: is: is: is: is: is: is: is: is: is: is: is: 0 1 2 3 4 5 6 7 8 9 : ; < = > ? 15 ASCII Confirmation Program #3 ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII

ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code # # # # # # # # # # # # # # # # 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 is:

is: is: is: is: is: is: is: is: is: is: is: is: is: is: is: @ A B C D E F G H I J K L M N O ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII Characters Lesson CS1313 Fall 2019 Code Code Code Code Code Code Code Code

Code Code Code Code Code Code Code Code # # # # # # # # # # # # # # # # 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 is: is: is: is: is: is: is: is: is: is: is: is: is: is: is: is: P

Q R S T U V W X Y Z [ \ ] ^ _ 16 ASCII Confirmation Program #4 ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code # 96 # 97 # 98 # 99 #100 #101 #102 #103

#104 #105 #106 #107 #108 #109 #110 #111 is: is: is: is: is: is: is: is: is: is: is: is: is: is: is: is: a b c d e f g h i j k l m n o ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII ASCII Characters Lesson CS1313 Fall 2019

Code Code Code Code Code Code Code Code Code Code Code Code Code Code Code #112 #113 #114 #115 #116 #117 #118 #119 #120 #121 #122 #123 #124 #125 #126 is: is: is: is: is: is: is: is: is: is: is: is: is: is: is: p q r s t u v w x y z {

| } ~ 17 A char is an int #1 #include int main () { /* main */ const int first_printable_character_code = 32; const int last_printable_character_code = 126; const int program_success_code = 0; int index; for (index = first_printable_character_code; index <= last_printable_character_code; index++) { printf("ASCII Code #%3d is: %c\n", index, index); } /* for index */ return program_success_code; } /* main */ Notice that the variable named index is declared as an int, but in the printf statement, index can be used not only as an int but also as a char. The reverse is also true. Characters Lesson CS1313 Fall 2019 18 A char is an int #2 #include int main () { /* main */ const int program_success_code = 0; const char first_printable_character_code = 32; const char last_printable_character_code = 126; char index; for (index = first_printable_character_code; index <= last_printable_character_code; index++) { printf("ASCII Code #%3d is: %c\n", index, index); } /* for index */ return program_success_code; } /* main */ Notice that the variable named index is declared as a char, but in the printf statement, index can be used not only as a char but also as an int. The reverse is also true. Characters Lesson CS1313 Fall 2019 19 Declaring char Scalar Variables #1

Heres a declaration of a char scalar variable: char first_initial; This declaration tells the compiler to grab a group of bytes, name them first_initial, and think of them as storing a char. How many bytes in a char scalar? Each char scalar takes one byte: first_initial : Characters Lesson CS1313 Fall 2019 20 Declaring char Scalar Variables #2 char first_initial; first_initial : REMEMBER: A char is just like an int, except that it uses fewer bytes: typically, a char is 1 byte and an int is 4 bytes. So, we can use char variables and constants in exactly the same ways that we use int variables and constants. Characters Lesson CS1313 Fall 2019 21 char Like int Example % cat charadd.c #include int main () { /* main */ const int program_success_code = 0; int addend, augend; char sum; printf("What are the addend and augend?\n"); scanf("%d %d", &addend, &augend); sum = addend + augend; printf("The sum is %d.\n", sum); return program_success_code; } /* main */ % gcc -o charadd charadd.c % charadd What are the addend and augend? 1 4 The sum is 5. Characters Lesson CS1313 Fall 2019 22 char Scalar Literal Constants A character scalar literal constant is a single char enclosed in single quotes: 'H' Note that ''' is illegal.

However, you can also represent an individual char literal using the octal (base 8) code that represents it. For example, the apostrophe character corresponds to ASCII code 39 decimal, which converts to 47 octal. So we can represent the apostrophe character like so: '\047' Characters Lesson CS1313 Fall 2019 23 char Scalar Literal Constant Example % cat apostrophe.c #include int main () { /* main */ const int program_success_code = 0; printf("Apostrophe: %c\n", '\047'); return program_success_code; } /* main */ % gcc -o apostrophe apostrophe.c % apostrophe Apostrophe: ' Characters Lesson CS1313 Fall 2019 24 Using char Scalar Variables In C, we can use char scalar variables in many of the same ways that we use int scalar variables. As we saw, for example, we can declare them: char first_initial; We can also assign char scalar values to char scalar variables, by enclosing them in single quotes: first_initial = 'H'; We can output char scalar values from char scalar variables, like so: printf("My first initial is %c.\n", first_initial); Characters Lesson CS1313 Fall 2019 25 Using char Scalar Variables Example % cat charscalar.c #include int main () { /* main */ const char computers_favorite_character = 'q'; const int program_success_code = 0; char users_favorite_character; printf("What is your favorite character?\n"); scanf("%c", &users_favorite_character); printf("Your favorite character is '%c'.\n", users_favorite_character);

printf("My favorite character is '%c'.\n", computers_favorite_character); return program_success_code; } /* main */ % gcc -o charscalar charscalar.c % charscalar What is your favorite character? Z Your favorite character is 'Z'. My favorite character is 'q'. Characters Lesson CS1313 Fall 2019 26 char Arrays #1 In C, you can have an array of type char, just as you can have arrays of numeric types: char my_name[12]; We can fill this char array with characters and be able to print them out. Characters Lesson CS1313 Fall 2019 27 char Arrays #2 my_name[ 0] my_name[ 1] my_name[ 2] my_name[ 3] my_name[ 4] my_name[ 5] my_name[ 6] my_name[ 7] my_name[ 8] my_name[ 9] my_name[10] my_name[11] = = = = = = = = = = = = 'H'; 'e'; 'n'; 'r'; 'y'; ' '; 'N';

'e'; 'e'; 'm'; 'a'; 'n'; Is this a good solution? Characters Lesson CS1313 Fall 2019 28 Character Array Example #1 #include int main () { /* main */ const int my_name_length = 12; char my_name[my_name_length]; int index; my_name[ 0] = 'H'; my_name[ 1] = 'e'; my_name[ 2] = 'n'; my_name[ 3] = 'r'; my_name[ 4] = 'y'; my_name[ 5] = ' '; my_name[ 6] = 'N'; my_name[ 7] = 'e'; my_name[ 8] = 'e'; my_name[ 9] = 'm'; my_name[10] = 'a'; my_name[11] = 'n'; printf("My name is "); for (index = 0; index < my_name_length; index++) { printf("%c", my_name[index]); } /* for index */ printf(".\n"); return 0; Characters Lesson } /* main */ CS1313 Fall 2019 29 Character Array Example #2 % gcc -o chararray chararray.c % chararray My name is Henry Neeman. This is an improvement, but its still not an efficient way to assign a sequence of characters to a variable. What we want is a kind of char variable whose use will be convenient for inputting, outputting and using sequences of characters. Characters Lesson CS1313 Fall 2019 30

Recently Viewed Presentations

  • A TOUR of WileyPLUS Use this after you

    A TOUR of WileyPLUS Use this after you

    Just click NEXT to go on. ... Register for your class through our new Course Finder! To use Course Finder go to . www.wileyplus.com. and click on the GET STARTED button. 2. Confirm that the section matches your schedule before...
  • OUR SOLAR SYSTEM - The World of Teaching

    OUR SOLAR SYSTEM - The World of Teaching

    Title: OUR SOLAR SYSTEM Author: GB Last modified by: Dad Created Date: 5/10/2006 6:06:35 PM Document presentation format: On-screen Show Other titles
  • An insurers&#x27; guide to what makes a fair sickness absence ...

    An insurers' guide to what makes a fair sickness absence ...

    A term could be implied into the contract, either on the officious bystander or the business efficacy tests of implied contractual incorporation, that "once the employee has become entitled to payment of disability income due under the long-term disability plan,...
  • Acls Ch05

    Acls Ch05

    chapter 5 culture and health care
  • HPS Propulsion Poster - Mick Peterson

    HPS Propulsion Poster - Mick Peterson

    The final design consists of the four solar panels using to newly purchased regulator to charge a 24V battery bank. A wiring diagram for the system is shown below. Future . Work. The system should be installed and running at...
  • SETPOINT Berkshire - heathen history

    SETPOINT Berkshire - heathen history

    Tsar Alexander III 1881 - 1894 Aims, Methods & Outcomes AIMS To secure the throne: To restore traditional autocracy. METHODS The 'Reaction' - increased repression But allowed Witte to commence 'the Great Spurt' OUTCOMES Re-established secure government (but Trotsky argued...
  • Chapter 3 Setting Effective Goals and Making the

    Chapter 3 Setting Effective Goals and Making the

    Received Goal Setting Training Did not receive Goal Setting Training 2.91 2.25. The ResultsMorisano, Hirsh, Peterson, Pihl & Shore (2010) Goal group less likely to drop classes. Goal group had . fewer negative emotions.
  • به نام خدا - Shahid Sadoughi University of Medical ...

    به نام خدا - Shahid Sadoughi University of Medical ...

    این فایل راهنما جهت آشنایی کاربران گرامی با پایگاه اطلاعاتی ebsco و نحوه جستجوی اطلاعات در آن تهیه شده است ...