Characters & Strings in C

Similar documents
1. Character/String Data, Expressions & Intrinsic Functions. Numeric Representation of Non-numeric Values. (CHARACTER Data Type), Part 1

Characters Lesson Outline

ASCII Code - The extended ASCII table

2a. Codes and number systems (continued) How to get the binary representation of an integer: special case of application of the inverse Horner scheme

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

FD-011WU. 2D Barcode Reader User Guide V1.6CC

Number Systems II MA1S1. Tristan McLoughlin. November 30, 2013

OOstaExcel.ir. J. Abbasi Syooki. HTML Number. Device Control 1 (oft. XON) Device Control 3 (oft. Negative Acknowledgement

Chapter 3. Information Representation

DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes. Error Detection Codes

MK D Imager Barcode Scanner Configuration Guide

EXPERIMENT 8: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

EXPERIMENT 7: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

Fundamentals of Programming (C)

Review. Single Pixel Filters. Spatial Filters. Image Processing Applications. Thresholding Posterize Histogram Equalization Negative Sepia Grayscale

The following are the data types used in the C programming language:

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Data Representation and Binary Arithmetic. Lecture 2

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Chapter 8. Characters and Strings

2D Barcode Reader User Guide V 1.2.1

S-Series Sensor ASCII Protocol v8.1.0

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

Fundamentals of Programming

Fundamental Data Types

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Do not start the test until instructed to do so!

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

BD-6500BT Bluetooth 2D Barcode Scanner Configuration Guide

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, SPRING 2013

Number System (Different Ways To Say How Many) Fall 2016

CS341 *** TURN OFF ALL CELLPHONES *** Practice NAME

Number Representations

This is great when speed is important and relatively few words are necessary, but Max would be a terrible language for writing a text editor.

Do not start the test until instructed to do so!

Chapter 2 Bits, Data Types, and Operations

Binary Numbers. The Basics. Base 10 Number. What is a Number? = Binary Number Example. Binary Number Example

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

Character Strings Lesson Outline

Mounting Dimensions / Viewing 2 Mounting Options 3. Wiring Configuration 4. Quick Set up Procedure 5. Changing Intensity 6.

EE 109 Unit 3. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Chapter 2 Bits, Data Types, and Operations

If your CNC machine memory is full, the PocketDNC gives you more storage, enabling you to store and reload proven programs at a later date.

ASSIGNMENT 5 TIPS AND TRICKS

Chapter 2 Bits, Data Types, and Operations

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

n NOPn Unary no operation trap U aaa NOP Nonunary no operation trap i

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

Serial I/O. 4: Serial I/O. CET360 Microprocessor Engineering. J. Sumey

PlainDSP M2M Communication Experimental Details This document describes the machine-to-machine (M2M) communication experiments using PlainDSP.

Variables and data types

2D Hand-held Barcode Scanner User Guide

Do not start the test until instructed to do so!

5/17/2009. Digitizing Discrete Information. Ordering Symbols. Analog vs. Digital

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Oberon Data Types. Matteo Corti. December 5, 2001

Unit 3. Analog vs. Digital. Analog vs. Digital ANALOG VS. DIGITAL. Binary Representation

APPENDIX B. ASSEMBLER DIRECTIVE SUMMARY

+ Inheritance. n Superclass (base class) higher in the hierarchy. n Subclass (child class) lower in the hierarchy

3.1. Unit 3. Binary Representation

Lecture (09) x86 programming 8

Chapter 2 Number System

Chapter 2 Bits, Data Types, and Operations

Midterm Exam, Fall 2015 Date: October 29th, 2015

EE 109 Unit 2. Binary Representation Systems

Simple Data Types in C. Alan L. Cox

PureScan - ML1. Configuration Guide. Wireless Linear Imager Wireless Laser scanner - 1 -

IPDA014-2D. Embedded 2D Barcode Scan Engine. User Guide

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

SPEECH RECOGNITION COMMON COMMANDS

The Binary Number System

Positional Number System

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

PD1100 STAND-ALONE PROGRAMMING & USER S GUIDE. use the freedom

CSC 8400: Computer Systems. Represen3ng and Manipula3ng Informa3on. Background: Number Systems

Chapter 2 Bits, Data Types, and Operations

Connecting UniOP to Datalogic Barcode Readers

CS 159 Credit Exam. What advice do you have for students who have previously programmed in another language like JAVA or C++?

Table of Contents Sleep Settings How to Configure the Scanner. 7 Chapter 2 System Setup

TELELINK I. A Warner Communications CompanyCI

User s Manual. Xi3000 Scanner. Table of Contents

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

FA269 - DIGITAL MEDIA AND CULTURE

Universal Asynchronous Receiver Transmitter Communication

6.096 Introduction to C++ January (IAP) 2009

Fundamentals of Programming

Password Management Guidelines for Cisco UCS Passwords

CSE 30 Winter 2014 Final Exam

SECURITY PROTECTION OF SOFTWARE PROGRAMS BY INFORMATION SHARING AND AUTHENTICATION TECHNIQUES USING INVISIBLE ASCII CONTROL CODES*

ENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2

Transcription:

Recently, we saw this: Characters & Strings in C % cat charweird.c #include <stdio.h> main () { /* main */ char c; c = 75; printf("c = %d\n", c); printf("c = %c\n", c); if (c) { printf("c is true\n"); } /* if c */ else { printf("c is false\n"); } /* if c...else */ } /* main */ % cc -o charweird charweird.c % charweird c = 75 c = K c is true We understood how the variable c could be treated as an integer, by accepting that the type char really just refers to the number of bytes that the data takes up (in this case, 1 byte). We also understood how the variable c could be treated as an Boolean, because Boolean values are implemented in C by using integers, with 0 denoting False and nonzero denoting True. But how does setting c to the integer value 75 give us a result of K? 1

Numeric Representation of Non-numeric Values In Programming Project #3, we encoded (represented) filing status with an integer; specifically: 1: single status 2: married filing jointly If we wanted to add other filing statuses, we could have had, for example: 3: married filing separately 4: head of household 5: widow/er Likewise in Programming Project #4, some people encoded the letter grades like so: 1: A 2: B 3: C 4: D 5: F And others encoded them like so: 4: A 3: B 2: C 1: D 0: F So, we can encode qualitative (non-numeric) variables with quantitative (numeric) values, using arbitrary but distinct numeric values to encode a set of qualities. 2

How Characters Are Represented 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 by writing. Writing is based on characters. Characters are non-numeric. So, we want a way to encode characters numerically. 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 26 Now that you ve grown up and taken CS1313, you realize that the numbers that you choose can be arbitrary, as long as they re 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? Well, you could add, for example: a 97, b 98, c 99, d 100,..., z 122 Are these the only characters that you need? 3

Digits & Punctuation 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 57 Notice 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. But wait, there s more! 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 chart. 4

The American Standard Code for Information Interchange (ASCII) 1 Code Char Kbd Name Code Char Kbd Name 0 NUL Null 16 DLE Ctrl-P Data Line Escape 1 SOH Ctrl-A Start Of Heading 17 DC1 Ctrl-Q Device Control 1 2 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 Ack 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 Code Char Name Code Char Name Code Char Name 32 space 64 @ at 96 accent grave 33! exclamation point 65 A 97 a 34 " double quote 66 B 98 b 35 # pound 67 C 99 c 36 $ dollar sign 68 D 100 d 37 % percent 69 E 101 e 38 & ampersand 70 F 102 f 39 single quote 71 G 103 g 40 ( open parenthesis 72 H 104 h 41 ) close parenthesis 73 I 105 i 42 * asterisk 74 J 106 j 43 + plus 75 K 107 k 44, comma 76 L 108 l 45 - hyphen 77 M 109 m 46. period 78 N 110 n 47 / slash 79 O 111 o 48 0 80 P 112 p 49 1 81 Q 113 q 50 2 82 R 114 r 51 3 83 S 115 s 52 4 84 T 116 t 53 5 85 U 117 u 54 6 86 V 118 v 55 7 87 W 119 w 56 8 88 X 120 x 57 9 89 Y 121 y 58 : colon 90 Z 122 z 59 ; semicolon 91 [ open square bracket 123 { open curly brace 60 < less than 92 \ backslash 124 vertical bar 61 = equal 93 ] close square bracket 125 } close curly brace 62 > greater than 94 ^ caret 126 ~ tilde 63? question mark 95 underscore 127 DEL delete 5

ASCII 1 Confirmation Example % cat ascii.c #include <stdio.h> main () { /* main */ int c; for (c = 32; c <= 63; c++) printf("%3d: %c %3d: %c %3d %c\n", c, c, c+32, c+32, c+64, c+64); } /* main */ % cc -o ascii ascii.c % ascii 32: 64: @ 96 33:! 65: A 97 a 34: " 66: B 98 b 35: # 67: C 99 c 36: $ 68: D 100 d 37: % 69: E 101 e 38: & 70: F 102 f 39: 71: G 103 g 40: ( 72: H 104 h 41: ) 73: I 105 i 42: * 74: J 106 j 43: + 75: K 107 k 44:, 76: L 108 l 45: - 77: M 109 m 46:. 78: N 110 n 47: / 79: O 111 o 48: 0 80: P 112 p 49: 1 81: Q 113 q 50: 2 82: R 114 r 51: 3 83: S 115 s 52: 4 84: T 116 t 53: 5 85: U 117 u 54: 6 86: V 118 v 55: 7 87: W 119 w 56: 8 88: X 120 x 57: 9 89: Y 121 y 58: : 90: Z 122 z 59: ; 91: [ 123 { 60: < 92: \ 124 61: = 93: ] 125 } 62: > 94: ^ 126 ~ 63:? 95: 127 1 Arthur Gill, Machine and Assembly Language Programming of the PDP-11, 2nd ed, Prentice- Hall, New Jersey, 1983, pp. 189-191. 6

Character Variables in C In C, we can declare variables of type char: char c; We can also assign character values to character variables, by enclosing them in single quotes: c = q ; We can output character values from character variables, using the %c placeholder: printf("my favorite character is: Here s an example: % cat charscalar.c #include <stdio.h> main () { /* main */ char mychar, yourchar; %c\n", c); mychar = q ; printf("what is your favorite character? "); scanf("%c", &yourchar); printf("your favorite character is: %c\n", yourchar); printf("my favorite character is: %c\n", mychar); } /* main */ % cc -o charscalar charscalar.c % charscalar What is your favorite character? z Your favorite character is: z My favorite character is: q The problem here is how much of a pain it d be to have to write words and sentences this way. 7

Character Arrays in C In C, you can have an array of type char, just as you can have arrays of numeric types: char myname[12]; We can fill this array with characters and be able to print them out: myname[0] = H ; myname[1] = e ; myname[2] = n ; myname[3] = r ; myname[4] = y ; myname[5] = ; myname[6] = N ; myname[7] = e ; myname[8] = e ; myname[9] = m ; myname[10] = a ; myname[11] = n ; We can put this to use like so: % cat chararray.c #include <stdio.h> main () { /* main */ char myname[12]; int c; myname[0] = H ; myname[1] = e ; myname[2] = n ; myname[3] = r ; myname[4] = y ; myname[5] = ; myname[6] = N ; myname[7] = e ; myname[8] = e ; myname[9] = m ; myname[10] = a ; myname[11] = n ; printf("my name is: "); for (c = 0; c < 12; c++) printf("%c", myname[c]); printf("\n"); } /* main */ % cc -o chararray chararray.c % chararray My name is: Henry Neeman This is an improvement, but it s still not an efficient way to store a sequence of characters in memory. 8

Character Strings in C In C A character string is a sequence of characters that is interpreted as a single item of data. In C, a character string is an array of characters terminated by a null character (ASCII value 0); i.e., the last character in the array is a null. % cat chararraystr.c #include <stdio.h> main () { /* main */ char myname[13]; char mynamestr1[13] = "Henry Neeman"; char mynamestr2[13]; int c; myname[0] = H ; myname[1] = e ; myname[2] = n ; myname[3] = r ; myname[4] = y ; myname[5] = ; myname[6] = N ; myname[7] = e ; myname[8] = e ; myname[9] = m ; myname[10] = a ; myname[11] = n ; myname[12] = \0 ; strcpy(mynamestr2, "Henry Neeman"); printf("my name is: "); for (c = 0; c < 12; c++) printf("%c", myname[c]); printf("\n"); printf("my name string is: %s\n", myname); printf("my name string is: %s\n", mynamestr1); printf("my name string is: %s\n", mynamestr2); } /* main */ % cc -o chararraystr chararraystr.c % chararraystr My name is: Henry Neeman My name string is: Henry Neeman My name string is: Henry Neeman My name string is: Henry Neeman In C, a character scalar is delimited by single quotes and its placeholder is %c, while a character string is delimited by double quotes, and its placeholder is %s. 9

Character Strings in Fortran 90 Just as in C, in Fortran 90 we have character strings, but they are declared with an additional attribute: CHARACTER (LEN=12) :: myname % cat chararraystrf.f90 PROGRAM chararraystrf IMPLICIT NONE CHARACTER,DIMENSION(1:12) :: myname CHARACTER (LEN=12) :: mynamestr1 = "Henry Neeman" CHARACTER (LEN=12) :: mynamestr2 INTEGER :: c myname(1) = "H" myname(2) = "e" myname(3) = "n" myname(4) = "r" myname(5) = "y" myname(6) = " " myname(7) = "N" myname(8) = "e" myname(9) = "e" myname(10) = "m" myname(11) = "a" myname(12) = "n" mynamestr2 = "Henry Neeman" PRINT *, "My name is: ", (myname(c), c = 1, 12) PRINT *, "My name is: ", mynamestr1 PRINT *, "My name is: ", mynamestr2 END PROGRAM chararraystrf % f90 -o chararraystrf chararraystrf.f90 % chararraystrf My name is: Henry Neeman My name is: Henry Neeman My name is: Henry Neeman In Fortran 90, you can delimit both character scalars and character strings with either single quotes or double quotes. The only constraint is that the delimiters must be a matched pair; that is, both delimiters must be single quotes, or both delimiters must be double quotes. 10

Character String Expressions in Fortran 90 Fortran 90 has some pretty powerful intrinsic string operators and functions: 1. Substrings: string(start:end) 2. Concatenation: str1 // str2 3. Creation: REPEAT(str,count) 4. Modification (a) Trailing blank removal: TRIM(str) (b) Left justify: ADJUSTL(str) removes leading blanks, adds an equal number of trailing blanks (c) Right justify: ADJUSTL(str) removes trailing blanks, adds an equal number of leading blanks 5. Length (a) Total length: LEN(str) (b) Length before trailing blanks: LEN TRIM(str) 6. Encoding/Decoding (a) Encode character to ASCII: IACHAR(chr) (b) Encode character to native code: ICHAR(chr) (c) Encode character from ASCII: ACHAR(integ) (d) Encode character from native code: CHAR(integ) 7. Comparison (a) Greater than: LGT(str1,str2) (b) Greater than or equal to: LGE(str1,str2) (c) Less than: LLT(str1,str2) (d) Less than or equal to: LLE(str1,str2) You can find a complete listing of string manipulators in Programming in Fortran 90/95, 6th ed, pages 113-117. 11

F90 Character String Expression Example % cat strintrinsics.f90 PROGRAM strintrinsics IMPLICIT NONE CHARACTER (LEN=12) myname CHARACTER (LEN=3) mydegree CHARACTER (LEN=10) mynose myname = "Henry Neeman" mydegree = "PhD" mynose = "bulbous" PRINT *, myname(7:9) PRINT *, myname // ", " // mydegree PRINT *, REPEAT(mydegree,3) PRINT *, " " // TRIM(mynose) // " " PRINT *, " " // ADJUSTL(mynose) // " " PRINT *, " " // ADJUSTR(mynose) // " " PRINT *, LEN(mynose), LEN_TRIM(mynose) PRINT *, IACHAR("A") =, IACHAR("A") PRINT *, ICHAR("A") =, ICHAR("A") PRINT *, ACHAR(65) =, ACHAR(65) PRINT *, CHAR(65) =, CHAR(65) PRINT *, myname, " LGT ", mydegree, " = ", & & LGT(myname,mydegree) PRINT *, myname, " LGE ", mydegree, " = ", & & LGE(myname,mydegree) PRINT *, myname, " LGE ", myname, " = ", & & LGE(myname,myname) PRINT *, myname, " LLT ", mydegree, " = ", & & LLT(myname,mydegree) PRINT *, myname, " LLE ", mydegree, " = ", & & LLE(myname,mydegree) PRINT *, myname, " LLE ", myname, " = ", & & LLE(myname,myname) END PROGRAM strintrinsics % f90 -o strintrinsics strintrinsics.f90 % strintrinsics Nee Henry Neeman, PhD PhDPhDPhD bulbous bulbous bulbous 10 7 IACHAR("A") = 65 ICHAR("A") = 65 ACHAR(65) = A CHAR(65) = A Henry Neeman LGT PhD = F Henry Neeman LGE PhD = F Henry Neeman LGE Henry Neeman = Henry Neeman LLT PhD = T Henry Neeman LLE PhD = T Henry Neeman LLE Henry Neeman = 12 T T

C Character String Expression Example % cat strfuncs.c #include <stdio.h> main () { /* main */ char myname[13] = "Henry Neeman"; char mydegree[4] = "PhD"; char mynose[11] = "bulbous"; char tempstr[100]; tempstr[0] = \0 ; strcat(tempstr, myname); strcat(tempstr, ", "); strcat(tempstr, mydegree); printf("%s\n", tempstr); tempstr[0] = \0 ; strcat(tempstr, mydegree); strcat(tempstr, mydegree); strcat(tempstr, mydegree); printf("%s\n", tempstr); printf("strlen(%s) = %d\n", myname, strlen(myname)); printf(" %c == %d\n", A, A ); printf("strcmp(%s,%s) == %d\n", myname, mydegree, strcmp(myname, mydegree)); printf("strcmp(%s,%s) == %d\n", myname, myname, strcmp(myname, myname)); printf("strcmp(%s,%s) == %d\n", myname, mynose, strcmp(myname, mynose)); printf("strcmp(%s,%s) == %d\n", mynose, mydegree, strcmp(mynose, mydegree)); } /* main */ % cc -o strfuncs strfuncs.c % strfuncs Henry Neeman, PhD PhDPhDPhD strlen(henry Neeman) = 12 A == 65 strcmp(henry Neeman,PhD) == -1 strcmp(henry Neeman,Henry Neeman) == 0 strcmp(henry Neeman,bulbous) == -1 strcmp(bulbous,phd) == 1 13

Arrays of Character Strings in C Just as you can have an array of type int, or an array of type float, or an array of type char, you can also have an array of char strings. Of course, an individual string is an array of type char. So an array of strings is an array of arrays of type char; that is, it s a 2D array of type char. % cat stringarray.c #include <stdio.h> #define NUM_NAMES 5 #define MAX_NAME_LENGTH 64 main () { /* main */ char name[num_names][max_name_length] = { "Nelson Mandela", "Jerry Yang", "Madeleine Albright", "Jennifer Lopez", "Albert Einstein" }; int s; printf("here are your names:\n"); for (s = 0; s < NUM_NAMES; s++) printf("%2d: %s\n", s, name[s]); } /* main */ % cc -o stringarray stringarray.c % stringarray Here are your names: 0: Nelson Mandela 1: Jerry Yang 2: Madeleine Albright 3: Jennifer Lopez 4: Albert Einstein 14