1 Chapter Nine Characters and Strings. 2 Text Data These days, computers work less with numeric data than with text data To unlock the full power of text.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

1 Chapter Ten Modular Development. 2 Stepwise Refinement At first, simple programs consist of only one source file and one function (the function main.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
Lab 10 rRepresentation And Conversion of Numeric Types l Difference between Numeric Types l Automatic conversion of Data types l Explicit Conversion of.
1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Data types and variables
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
1 Character Processing How characters can be treated as small integers? How characters are stored and manipulated in a machine? How use is made of certain.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Data Types.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Chapter 8 Characters and Strings. Principle of enumeration Computers tend to be good at working with numeric data. The ability to represent an integer.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 8 CHARACTER AND STRINGS
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
February 14, 2005 Characters, Strings and the String Class.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Announcements Final NEXT WEEK (August 13 th Thursday at 16:00) Recitations will be held on August 12 th Wednesday We will solve sample final questions.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Data Types & I/O Streams Objectives Data Types Understand that integers form the underlying foundation for many data types. Introduce a few frequently.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
DATA TYPE AND DISPLAY Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration.
Characters and Strings
Jordan Jozwiak CS50. Announcements Pset3 will be returned by 7pm on Tuesday REMINDER: Access section materials from this year and last year at
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Introduction to ‘c’ language
Character Processing How characters can be treated as small integers?
7. BASIC TYPES.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Basic Elements of C++.
Chapter 8 - Characters and Strings
Primitive Types Vs. Reference Types, Strings, Enumerations
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Basics of ‘C’.
Chapter 2: Introduction to C++.
Character Processing How characters can be treated as small integers?
Lexical Elements & Operators
Presentation transcript:

1 Chapter Nine Characters and Strings

2 Text Data These days, computers work less with numeric data than with text data To unlock the full power of text data, you need to know how to manipulate strings in more sophisticated ways Because a string is composed of individual characters, it is important for you to understand how character work and how they are represented inside the computer

3 Enumeration Types There are many types of useful data that are neither numeric data nor text data The days of a week: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday The classes of students in the school: freshman, sophomore, junior, senior

4 Enumeration Types The process of listing all the elements in the domain of a type is called enumeration A type defined by listing all of its elements is called an enumeration type Characters are similar in structure to enumeration types

5 Representing Enumeration Types How do computers represent internally the values of enumeration types Computers are good at manipulating numbers To represent a finite set of values of any type, all you have to do is to give each value a number The process of assigning an integer to each element of an enumeration type is called integer encoding

6 An Example #defineSunday0 #defineMonday1 #define Tuesday2 #defineWednesday3 #defineThursday4 #defineFriday5 #defineSaturday6 #defineFreshman1 #defineSophomore2 #define Junior3 #defineSenior4 int weekday; int class;

7 Defining Enumeration Types A new enumeration type can be defined as typedef enum { list of elements } type-name; For example, typedef enum { FALSE, TRUE } bool;

8 An Example typedef enum { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } weekdayT; typedef enum { Freshman, Sophomore, Junior, Senior } classT;

9 Advantages The compiler is able to choose the integer codes, thereby freeing the programmer from the responsibility A separate and meaningful type name instead of int makes the program easier to read Explicitly defined enumeration types are easier to debug

10 Integer Encoding You can specify explicitly the integer codes associated with the elements of an enumeration type as part of the definition If an element is not explicitly assigned an integer code, a consecutive integer code next to the previous element is assigned By default, the integer codes for the elements start with 0

11 An Example typedef enum { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } weekdayT; typedef enum { Freshman = 1, Sophomore, Junior, Senior } classT; typedef enum { FALSE, TRUE } bool;

12 Operations on Enumeration C compilers automatically convert values of an enumeration type to integers whenever the values are used in an expression All arithmetic for enumeration types works the same way as it does for integers However, compilers do not check if the value of an expression is still a valid value of an enumeration type weekday = (weekday + 1) % 7;

13 An Example typedef enum { North, East, South, West } directionT; directionT OppositeDirection(directionT dir) { switch (dir) { case North: return South; case East: return West; case South: return North; case West: return East; default: printf(“Illegal direction value.”); }

14 Characters In C, single characters are represented using the type char The type char is a built-in enumeration type The domain of values of char is the set of symbols that can be displayed on the screen or typed on the keyboard The set of operations for char is the same as that for int

15 ASCII Character Set To allow effective communication among computers, standard integer encoding systems for characters have been proposed The most commonly used system is the ASCII (American Standard Code for Information Interchange) character set

16 ASCII Character Set \000 \001 \002 \003 \004 \005 \006 \a \b \t 10 \n \v \f \r \016 \017 \020 \021 \022 \ \024 \025 \026 \027 \030 \031 \032 \033 \034 \ \036 \037 space ! “ # $ % & ‘ 40 ( ) * +, -. / : ; 60 A B C D E 70 F G H I J K L M N O 80 P Q R S T U V W X Y 90 Z [ \ ] ^ _ ` a b c 100 d e f g h i j k l m 110 n o p q r s t u v w 120 x y z { | } ~ \177

17 Character Constants Character constant is written by enclosing the desired character in single quotation marks ‘A’=>65 ‘9’=>57 Avoid using integer constants to refer to ASCII characters within a program

18 Properties of ASCII Set The codes for the digits 0 through 9 are consecutive The codes for the uppercase letters are consecutive The codes for the lowercase letters are consecutive

19 Special Characters The characters that can be displayed on the screen are called printing characters The other characters that are used to perform a particular operation are called special characters Special characters are represented as escape sequences that consist of a backslash ‘\’ followed by a letter or an octal numeric value

20 Escape Sequence \aAudible alert (beeps or rings a bell) \bBackspace \fFormfeed (starts a new page) \nNewline (moves to the beginning of the next line) \rReturn (returns to the beginning of the current line) \tTab (moves horizontally to the next tab stop) \vVertical tab (moves vertically to the next tab stop) \0Null character (the character whose ASCII code is 0) \\The character \ itself \ ’ The character ’ (only in character constants) \ ” The character ” (only in string constants) \dddThe character whose ASCII code is octal number ddd

21 Character Arithmetic Adding an integer to a character ‘0’ + 5 => ‘5’,‘A’ + 5 => ‘F’ Subtracting an integer from a character ‘5’ – 5 => ‘0’,‘F’ – 5 => ‘A’ Subtracting one character from another ‘X’ + (‘a’ – ‘A’) => ‘x’ Comparing two characters against each other ‘F’ > ‘A’ => TRUE, ‘F’ > ‘f’ => FALSE

22 Types of Characters The ctype.h interface declares several predicate functions for determining the type of a character islower(ch) TRUE if ch is a lowercase isupper(ch) TRUE if ch is a uppercase isalpha(ch) TRUE if ch is a letter isdigit(ch) TRUE if ch is a digit isalnum(ch) TRUE if ch is a letter or digit ispunct(ch) TRUE if ch is a punctuation isspace(ch) TRUE if ch is ‘ ’, ‘\f’, ‘\n’, ‘\t’, or ‘v’

23 An Example bool islower(char ch) { return (ch >= ‘a’ && ch <= ‘z’); } bool isdigit(char ch) { return (ch >= ‘0’ && ch <= ‘9’); }

24 Conversion of Letters The ctype.h interface also declares two extremely useful conversion functions tolower(ch): If ch is an uppercase letter, returns its lowercase equivalent; otherwise returns ch unchanged toupper(ch): If ch is an lowercase letter, returns its uppercase equivalent; otherwise returns ch unchanged

25 An Example char tolower(char ch) { if (ch >= ‘A’ && ch <= ‘Z’) { return ch + (‘a’ – ‘A’); } else { return ch; }

26 Reasons for Using Libraries Because the library functions are standard, it is easier for other programmers to read library functions than your own It is easier to rely on library functions for correctness than on your own The library implementation of functions are often more efficient than your own

27 Characters in Switch bool isVowel(char ch) { switch (tolower(ch)) { case ‘a’: case ‘e’: case ‘i’: case ‘o’: case ‘u’: return TRUE; default: return FALSE; }

28 Character Input & Output Character input is performed using int getchar(void); in stdio.h. It returns the character read or EOF if end of file or error occurs Character output is performed using int putchar(ch); in stdio.h. It returns the character written or EOF if error occurs

29 An Example A cyclic letter-substitution cipher: Cipher code = 4 I am a student from Taiwan. M eq e wxyhirx jvsq Xemaer.

30 An Example main() { int k, ch; printf(“Key in cipher code? ”); scanf(“%d”, &k); while ((ch = getchar()) != EOF) { if (isupper(ch)) { ch = (ch – ‘A’ + k) % 26 + ‘A’; } else if (islower(ch)) { ch = (ch – ‘a’ + k) % 26 + ‘a’; } putchar(ch); }

31 Strings A string is a sequence of characters In this chapter, you will learn the abstract behaviors of strings by using a string library that defines a type string and hides the internal representation of strings and many manipulations of strings, just like int and double You will learn those complex details in the later chapters

32 Layered Abstraction The strlib.h library The ANSI C string.h library ANSI C language-level operations Machine-level operations increasing detail increasing abstraction

33 Abstract Types An abstract type is a type defined only by its behavior and not in terms of its representation The behavior of an abstract type is defined by the operations that can be performed on objects of that type. These operations are called primitive operations

34 The strlib.h Library This library contains the following functions getLine()read a line as a string stringLength(s)length of a string ithChar(s, i)ith character of a string concat(s 1, s 2 )concatenates two strings copyString(s)copy a string substring(s, p 1, p 2 )extract a substring stringEqual(s 1, s 2 )Are two strings equal stringCompare(s 1, s 2 )compare two strings charToString(ch)convert char to string

35 The strlib.h Library This library contains the following functions findChar(ch, str, p)find a character findString(s, str, p)find a substring convertToLowerCase(s)converts to lowercase convertToUpperCase(s) converts to uppercase intToString(i) converts integer to string realToString(ch) converts real to string stringToInt(s) converts string to integer stringToReal(s) converts string to real

36 getLine & stringLength main() { string str; printf(“Key in a string: ”); str = getLine(); printf(“The length of %s is %d.\n”, str, stringLength(str)); }

37 ithChar /* “student” => ‘t’ */ char lastChar(string str) { return (ithChar(str, stringLength(str) - 1); } /* The positions within a string are numbered starting from 0 */

38 concat string concatNCopies(int n, string str) { string result; int i; result = “”; for (i = 0; i < n; i++) result = concat(result, str); return result; } /* (4, “*”) => “****” */

39charToString string reverseString(string str) { string result, temp; int i; result = “”; for (i = 0; i < stringLength(str); i++) { temp = charToString(ithChar(str, i)); result = concat(temp, result); } return result; } /* “student” => “tneduts” */

40 subString string secondHalf(string str) { int len; len = stringLength(str); return subString(str, len / 2, len - 1); }

41 subString If p1 is negative, it is set to 0 so that it indicates the first character in the string If p2 is greater than stringLength(s) - 1, it is set to stringLength(s) – 1 so that it indicates the last character If p1 ends up being greater than p2, subString returns the empty string

42 stringEqual main() { string answer; while (TRUE) { playOneGame(); printf(“Would you like to play again? ”); answer = getLine(); if (stringEqual(answer, “no”)) break; }

43 stringCompare If s1 precedes s2 in lexicographic order, stringCompare returns a negative integer If s1 follows s2 in lexicographic order, stringCompare returns a positive integer If the two string are exactly the same, stringCompare returns 0 The lexicographic order is different from the alphabetical order used in dictionaries

44 findChar string Acronym(string str) { string acronym; int pos; acronym = charToString(ithChar(str, 0)); pos = 0; while (TRUE) { pos = findChar(‘ ’, str, pos + 1); if (pos == -1) break; acronym = concat(acronym, charToString(ithChar(str, pos + 1))); } return acronym; } /* “Chung Cheng University” => “CCU” */

45 findString string replaceFirst(string str, string pat, string replace) { string head, tail; int pos; pos = findString(pat, str, 0); if (pos == -1) return str; head = subString(str, 0, pos - 1); tail = subString(str, pos + stringLength(pat), stringLength(str) - 1); return concat(concat(head, replace), tail); } replaceFirst(“a plan”, “a”, “a nice”) => “a nice plan”

46 convertToLowerCase string convertToLowerCase(string str) { string result; char ch; int i; result = “”; for (i = 0; i < stringLength(str); i++) { ch = ithChar(str, i); result = concat(result, charToString(tolower(ch))); } return result; }

47 Numeric Conversion The function intToString(n) converts the integer n into a string of digits, preceded by a minus sign if n is negative intToString(123) => “123” intToString(-4) => “-4” The function realToString(d) converts the floating point d into the string that would be displayed by printf using the %G format code realToString(3.14) => “3.14” realToString( ) => “1.5E-10”

48 protectedIntegerField string protectedIntegerField(int n, int places) { string numstr, fill; numstr = intToString(n); fill = concatNCopies(places - stringLength(numstr), “*”); return concat(fill, numstr); } *****123