Publications: 120 | Followers: 1

## CS1313 Characters and Strings Lesson 1

Publish on Category: Birds 0

Characters LessonCS1313 Spring 2019
1
Characters Lesson Outline
Characters Lesson OutlineNumeric Encoding of Non-numeric Data #1Numeric Encoding of Non-numeric Data #2Representing CharactersHow Characters Are Represented #1How Characters Are Represented #2Representing DigitsRepresenting PunctuationASCIIASCII Table #1ASCII Table #2ASCII Table #3ASCII Table #4ASCII Confirmation Program #1ASCII Confirmation Program #2ASCII Confirmation Program #3ASCII Confirmation Program #4
Acharis anint#1Acharis anint#2DeclaringcharScalar Variables #1DeclaringcharScalar Variables #2charLikeintExamplecharScalar Literal ConstantscharScalar Literal Constant ExampleUsingcharScalar VariablesUsingcharScalar Variables ExamplecharArrays #1charArrays #2Character Array Example #1Character Array Example #2
Characters LessonCS1313 Spring 2019
2
In Programming Project #4, weencoded(represented) entree item choices usingintegervalues:enchiladaburritoquesadillaIf we wanted to add other entrees, for example:chilerellenofajitas
Numeric Encoding of Non-numeric Data #1
Characters LessonCS1313 Spring 2019
3
enchiladaburritoquesadillachilerellenofajitas...The numbers in these cases have no standard meaning with respect to the items that they encode; they’ve been chosen essentially at random.So, we see that we can encodequalitative(non-numeric) values withquantitative(numeric) values, usingarbitrarybutdistinctnumeric values to encode a set of qualities.
Numeric Encoding of Non-numeric Data #2
Characters LessonCS1313 Spring 2019
4
Representing Characters
What’s the most important set of non-numeric values in computing?It’s 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 bywriting.Writing is based oncharacters.Characters arenon-numeric.So, we want a way toencodecharacters numerically.
Characters LessonCS1313 Spring 2019
5
How Characters Are Represented #1
Here’s 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' = 26Now that you’ve grown up and taken CS1313, you realize that the numbers that you choose can bearbitrary, as long as they’refixedanddistinct.So you could just as easily choose:'A' = 65, 'B' = 66, 'C' = 67, 'D' = 68, . . ., 'Z' = 90This is a perfectly reasonable encoding, if the only characters that you care about are upper case letters.What about lower case?
Characters LessonCS1313 Spring 2019
6
How Characters Are Represented #2
'A' = 65, 'B' = 66, 'C' = 67, 'D' = 68, . . ., 'Z' = 90What about lower case?Well, you could add, for example:'a' = 97, 'b' = 98, 'c' = 99, 'd' = 100, . . ., 'z' = 122Are these the only characters that you need?
Characters LessonCS1313 Spring 2019
7
Representing Digits
Another kind of very important character is a digit.Here’s a possible encoding of the decimal digits:'0' = 48, '1' = 49, '2' = 50, '3' = 51, . . ., '9' = 57Notice that there’s an important distinction between the character to be represented, which happens to be a digit, and the numeric encoding, whose value doesn’t have to have anything to do with the value of the digit being encoded.
Characters LessonCS1313 Spring 2019
8
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 it’d help to have a standardized system.
Characters LessonCS1313 Spring 2019
9
ASCII
TheAmerican Standard Code for Information Interchange(ASCII)*is a standardized system for encoding characters numerically.It has several categories of characters:letters:upper case ('A' = 65 through 'Z' = 90);lower case ('a' = 97 through 'z' = 122);digits ('0' = 48 through '9' = 57);punctuationspace = 32 through slash = 47;colon = 58 through at sign = 64;open square bracket = 91 throughbackquote= 96;open curly brace = 123 through tilde = 126;control characters, encoded as 0 through 31; alsoDEL(encoded as 127).*http://www.asciitable.com/
Characters LessonCS1313 Spring 2019
10
ASCII Table #1
Characters LessonCS1313 Spring 2019
11
ASCII Table #2
Characters LessonCS1313 Spring 2019
12
ASCII Table #3
Characters LessonCS1313 Spring 2019
13
ASCII Table #4
Characters LessonCS1313 Spring 2019
14
ASCII Confirmation Program #1
#include <stdio.h>intmain (){ /* main */constintfirst_printable_character_code= 32;constintlast_printable_character_code= 126;constintprogram_success_code= 0;intindex;for (index =first_printable_character_code;index <=last_printable_character_code;index++) {printf("ASCII Code #%3d is: %c\n",index, index);} /* for index */returnprogram_success_code;} /* main */
Characters LessonCS1313 Spring 2019
15
ASCII Confirmation Program #2
%gcc-oasciitestasciitest.c%asciitestASCII 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 Code # 48 is: 0ASCII Code # 49 is: 1ASCII Code # 50 is: 2ASCII Code # 51 is: 3ASCII Code # 52 is: 4ASCII Code # 53 is: 5ASCII Code # 54 is: 6ASCII Code # 55 is: 7ASCII Code # 56 is: 8ASCII Code # 57 is: 9ASCII Code # 58 is: :ASCII Code # 59 is: ;ASCII Code # 60 is: <ASCII Code # 61 is: =ASCII Code # 62 is: >ASCII Code # 63 is: ?
Characters LessonCS1313 Spring 2019
16
ASCII Confirmation Program #3
ASCII Code # 64 is: @ASCII Code # 65 is: AASCII Code # 66 is: BASCII Code # 67 is: CASCII Code # 68 is: DASCII Code # 69 is: EASCII Code # 70 is: FASCII Code # 71 is: GASCII Code # 72 is: HASCII Code # 73 is: IASCII Code # 74 is: JASCII Code # 75 is: KASCII Code # 76 is: LASCII Code # 77 is: MASCII Code # 78 is: NASCII Code # 79 is: O
ASCII Code # 80 is: PASCII Code # 81 is: QASCII Code # 82 is: RASCII Code # 83 is: SASCII Code # 84 is: TASCII Code # 85 is: UASCII Code # 86 is: VASCII Code # 87 is: WASCII Code # 88 is: XASCII Code # 89 is: YASCII Code # 90 is: ZASCII Code # 91 is: [ASCII Code # 92 is: \ ASCII Code # 93 is: ]ASCII Code # 94 is: ^ASCII Code # 95 is: _
Characters LessonCS1313 Spring 2019
17
ASCII Confirmation Program #4
ASCII Code # 96 is: ‘ASCII Code # 97 is: aASCII Code # 98 is: bASCII Code # 99 is: cASCII Code #100 is: dASCII Code #101 is: eASCII Code #102 is: fASCII Code #103 is: gASCII Code #104 is: hASCII Code #105 is: iASCII Code #106 is: jASCII Code #107 is: kASCII Code #108 is: lASCII Code #109 is: mASCII Code #110 is: nASCII Code #111 is: o
ASCII Code #112 is: pASCII Code #113 is: qASCII Code #114 is: rASCII Code #115 is: sASCII Code #116 is: tASCII Code #117 is: uASCII Code #118 is: vASCII Code #119 is: wASCII Code #120 is: xASCII Code #121 is: yASCII Code #122 is: zASCII Code #123 is: {ASCII Code #124 is: |ASCII Code #125 is: } ASCII Code #126 is: ~
Characters LessonCS1313 Spring 2019
18
Acharis anint#1
#include <stdio.h>intmain (){ /* main */constintfirst_printable_character_code= 32;constintlast_printable_character_code= 126;constintprogram_success_code= 0;intindex;for (index =first_printable_character_code;index <=last_printable_character_code;index++) {printf("ASCII Code #%3d is: %c\n",index, index);} /* for index */returnprogram_success_code;} /* main */Notice that the variable namedindexis declared as anint, but in theprintfstatement,indexcan be used not only as anintbut also as achar. The reverse is also true.
Characters LessonCS1313 Spring 2019
19
Acharis anint#2
#include <stdio.h>intmain (){ /* main */constintprogram_success_code= 0;constcharfirst_printable_character_code= 32;constcharlast_printable_character_code= 126;charindex;for (index =first_printable_character_code;index <=last_printable_character_code;index++) {printf("ASCII Code #%3d is: %c\n",index, index);} /* for index */returnprogram_success_code;} /* main */Notice that the variable namedindexis declared as achar, but in theprintfstatement,indexcan be used not only as acharbut also as anint. The reverse is also true.
Characters LessonCS1313 Spring 2019
20
DeclaringcharScalar Variables #1
Here’s a declaration of acharscalar variable:charfirst_initial;This declaration tells the compiler to grab a group of bytes, name themfirst_initial, and think of them as storing achar.How many bytes in acharscalar?Eachcharscalar takes one byte:
first_initial:
Characters LessonCS1313 Spring 2019
21
DeclaringcharScalar Variables #2
charfirst_initial;REMEMBER:Acharis just like anint, except that it uses fewer bytes: typically, acharis 1 byte and anintis 4 bytes.So, we can usecharvariables and constants in exactly the same ways that we useintvariables and constants.
first_initial:
Characters LessonCS1313 Spring 2019
22
charLikeintExample
Characters LessonCS1313 Spring 2019
23
charScalar Literal Constants
Acharacter scalar literal constantis a singlecharenclosed in single quotes:'H'Note that'''is illegal.However, you can also represent an individualcharliteral using theoctal(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 LessonCS1313 Spring 2019
24
charScalar Literal Constant Example
%catapostrophe.c#include <stdio.h>intmain (){ /* main */constintprogram_success_code= 0;printf("Apostrophe: %c\n", '\047');returnprogram_success_code;} /* main */%gcc-o apostropheapostrophe.c%apostropheApostrophe: '
Characters LessonCS1313 Spring 2019
25
UsingcharScalar Variables
In C, we can usecharscalar variables in many of the same ways that we useintscalar variables. As we saw, for example, we can declare them:charfirst_initial;We can also assigncharscalar values tocharscalar variables, by enclosing them in single quotes:first_initial= 'H';We can outputcharscalar values fromcharscalar variables, like so:printf("My first initial is %c.\n",first_initial);
Characters LessonCS1313 Spring 2019
26
UsingcharScalar Variables Example
%catcharscalar.c#include <stdio.h>intmain (){ /* main */constcharcomputers_favorite_character= 'q';constintprogram_success_code= 0;charusers_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);returnprogram_success_code;} /* main */%gcc-ocharscalarcharscalar.c%charscalarWhat is your favorite character?ZYour favorite character is 'Z'.My favorite character is 'q'.
Characters LessonCS1313 Spring 2019
27
charArrays #1
In C, you can have an array of typechar, just as you can have arrays of numeric types:charmy_name[12];We can fill thischararray with characters and be able to print them out.
Characters LessonCS1313 Spring 2019
28
charArrays #2
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';
Is this a good solution?
Characters LessonCS1313 Spring 2019
29
Character Array Example #1
#include <stdio.h>intmain (){ /* main */constintmy_name_length= 12;charmy_name[my_name_length];intindex;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;} /* main */
Characters LessonCS1313 Spring 2019
30
Character Array Example #2
%gcc-ochararraychararray.c%chararrayMy name is Henry Neeman.This is an improvement, but it’s still not an efficient way to assign a sequence of characters to a variable.What we want is a kind ofcharvariable whose use will be convenient for inputting, outputting and using sequences of characters.

0

Embed

Share