PRACTICAL DATA STRUCTURES USING C/C++ Chapter 2 Strings Tel: 04-7232105 ext.3242 彰師大 數學系 蔡政容 (Cheng-Jung Tsai)

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

Problem Solving & Program Design in C
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Character and String definitions, algorithms, library functions Characters and Strings.
Chapter 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 10.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
1 Chapter 10 Characters, Strings, and the string class.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 8 CHARACTER AND STRINGS
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
 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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Chapter 8 Arrays and Strings
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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’};
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
String operations. About strings To handle strings more easily, we need to include a library> #include To see what the library allows us to do, look here:
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
Strings, Pointers and Tools
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
1 Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character Handling Library 8.4String Conversion.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
C Characters and Strings
C Characters and Strings
A First Book of ANSI C Fourth Edition
CSE 303 Lecture 14 Strings in C
Chapter 8 - Characters and Strings
C++ Programming Lecture 20 Strings
Characters and Strings Functions
C Characters and Strings
Presentation transcript:

PRACTICAL DATA STRUCTURES USING C/C++ Chapter 2 Strings Tel: ext.3242 彰師大 數學系 蔡政容 (Cheng-Jung Tsai)

2 Outline  Introduction  2.1 characters and strings  2.2 initializing strings  2.3 passing strings between functions  2.4 working with string elements  2.5 string handling functions  2.6 sorting and processing strings  2.7 application program: text formatter

3 Introduction  Understand the relationships among characters, pointers, and strings. sorting.  Will help you in developing programs that are capable of many powerful operations, such as sorting.

4 2.1 characters and strings: Sorting strings character  A character can be thought of as a single memory location string  A string is nothing more than an arrangement of characters  See Fig. 2.1 in pages 19 NULL character (\0)  All character strings require the NULL character (\0) to let the complier know where the string ends  See Program 2.1 in pages 18  See Program 2.2 in pages 19

5 Where are the elements? a single memory location of one byte  In a string array, each element is a character and represents a single memory location of one byte  Pointer:  See Program 2.3 in page 20  *(ptr+1) vs. ptr+1: content+1 vs address+1  See Fig 2.3 in page 22  See Program 2.4 in page 22

6 2.2 initializing strings  Examine a number of ways a string of character may be initialized  char string[ ] = “hello”; Automatically reserves the correct number of memory locations, Including the final NULL character  char string[ ] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’, }; The same result to above, but is a stupid initilaization  char string[5] = “hello”; Use when it is necessary to specify the length of a string  Specifying more characters for the string than are required is common  char string[80] = “hello”;  ‘x’ vs. “x” :  character vs string  “x” = ‘x’ and ‘\0’

7 initializing strings  Another way to initialize a string is to reserve storage for it and then use built-in functions (in ) to read the string in  Two function to read the string  scanf(): terminates scanning when it encounters the blank character  gests():  See pages 24 to 25

8 2.3 passing strings between functions  Strings are nothing more than character arrays,  You can pass strings between functions by passing only a pointer to the first element of the string  See Program 2.5 and 2.6 in page 26  Passing string between function1() and main() in Pro. 2.5  Passing starting address of the string between function1() and main() in Pro. 2.6  See Program 2.7 in page 27  compare the length of two strings

9 2.4 working with string element  This section examines a number of built-in functions that allow us to input string elements, examine them, and even convert them from one form to another  getchar() See Program 2.8 in page 28  See Program 2.8 in page 28 See Program 2.9 in pages 28 to 29  See Program 2.9 in pages 28 to 29

10 working with string element Checking characters  Built-in functions that will check the type of character that is entered into a program  See Table 2.1 in page 30  example: isalpha() See Program 2.10 in page 29  See Program 2.10 in page 29

11 Program 2.11 in page 30  Asks for a string of text from the user and prints out the string minus any punctuation or numerical digits  Lowercase alphabetical character are converted to uppercase as well  Bulit-in functions used: ispunct() & isdigit() & islower() & 0xdf  string[i] = string[i] & 0xdf

12 ASCII Table

13

14

15 Lowercase  uppercase & 0xdf  string[i] = string[i] & 0xdf  0x: 16 進位  df:  a:61, A: 41  a:61 hex, A: 41 hex :  a  (61) hex : and and  41

16 Program 2.12 in page 31  Asks user to enter a signed integer and then proceeds to examine the input string and build a resulting integer value based on the ASCII characters entered.  number += string[i] – 0x30  0x30: (30) hex  0  Ex. (36) hex -(30) hex = 6  6-0  See Program 2.13:  See Program 2.13: Using built-in function atoi() when you are codeing your program  atof( )  ASCII to flot  atol( )  ASCII to long

String Handling Functions  Examples covered so far have dealt with operations on character strings on a character-by-character basis  What are the operations we might need to perform on an entire string?  Find the length of a string, combine two strings, compare two strings, search a string for a character...etc  Are provided in  See Table 2.2 in page 33  See Table 2.2 in page 33 to see a subset of the available string handling functions

18 String Handling Functions: e strlen(), strcat() and strncat  strlen()  Return the length of a string, but not including the NULL  See Program 2.14 in page 34  Implement strlen() function by your self code See Program 2.15 in page 35 See Program 2.15 in page 35  strcat(str1,str2) and strncat(str1,str2,n) See Fig 2.4  Concatenate two strings, See Fig 2.4  See Program 2.16 in page 36  Implement strcat() function by your self code See Program 2.17 in page 36 See Program 2.17 in page 36

19 String Handling Functions: strcmp() and strncmp()  strcmp() and strncmp()  Compare two strings Return 0 if they are identical <0 if the first string precedes the second >1 otherwise  See page 37 for examples Doe, Morries, Morrison, Smith See Program 2.18 in page 37  See Program 2.18 in page 37  Implement strcmp() function by your self code See Program 2.19 in page 38 See Program 2.19 in page 38 length = (a > b) ? a : b If a > b then a else b

20 String Handling Functions: strchr() and strrchr()  strchr(str1,str2) and strrchr(str1,str2)  Search a string for a specified character  Two arguments The first is the string to be searched The second is the character to search for  Both return a pointer to the character’s position within the string if found, or NULL if not found  strchr() return the position of the first occurrence  strrchr() return the position of the last occurrence  See Program 2.20 in page 40 for an example of strchr()

21 String Handling Functions: strstr() and strpbrk()  strstr(str1,str2)  Searches for the first occurrence of a substring within a string  return a pointer to the beginning of the substring  NULL if not found  See Program 2.21 in page 41 for an example  strpbrk(str1,str2)  Searches for the first occurrence of any character of a substring within a string  strpbrk(“good morning”,”time out”) Will return the pointer to the first ‘o’ in “good morning”  See Program 2.22 in page 42 for an example

22 String Handling Functions: strtod(), strtol() and strtoul()  Convert strings into double, long, unsigned long

sorting and processing strings  See Program 2.23 in page 43  Sort the list of names  Two-dimensional array is used This initialization in this example will automatically fills the remaining character position with NULL bubble sort in page 44 (see next slide for example)  Using bubble sort in page 44 (see next slide for example) Simple but inefficient (O(n 2 ))  Bulit-in function strcpy() is used  See Program 2.24 in page 44 ragged array  Similar to Program 2.23 but different definition of array (ragged array) is used (see page 46  ragged array is a more efficient storage method than rectangular array but is also harder to work with (see page 46)

24 Bubble sort  Original string  String after first loop

25 ISBN checker  See Program 2.25 in page 47  Checks a user-supplied ISBN code to determine if it is a valid sequence  See page 48  See page 48 for the format of an legal ISBN code  Note two statements total += (input[i] - 0x30) * (i + 1); printf(“c%”, (rem + 0x30) )

26 Vowel counter  See Program 2.26 in page 49  To reduce the number of comparisons required, each character from the input string is converted into uppercase if (isalpha(text[j]) != 0) & 0xdf text[j] = text[j] & 0xdf; /* Uppercase is still uppercase, but lowercase will become uppercase, see slide 15 */

27 Palindrome checker palindrome  A palindrome is a string of symbols that reads the same forward and backward  mom, otto,  Palindrome play an important role in the study of languages  See Program 2.27 in page 50  Note the use of two variables “lchar” and “rchar”  Note the use of variable “stopped “

28 A tokenizer tokenizer token  A tokenizer in complier breaks each line of source file into smallest parts of a language (token).  See Fig 2.7 in page 51  See Fig 2.7 in page 51 to understand the structure of a complier  See Program 2.28 in page 52 to understand the code of a limited tokenizer

29 Encoding text transposition encoding  One of the simplest technique is called transposition encoding  The input text is written as a two-dimensional array of characters  Then the array is transposed  Ex. In p.54 “c programming is fun”   Transposed array  output is “crunpagnrmiomsgif”  See Program 2.29 in page 55,  See Program 2.29 in page 55, please note that  variable i : the number of blanks, n: used to created a n*n array c p r o g r a m m i n g i s f u n c r n u p a g h r m i o m s g i f

application program: text formatter inserting the correct number of blanks between wordsall lines exactly fit between the predefined left and right margins.  The purpose of the text formatter is to adjust the way a block of text is displayed or printed by inserting the correct number of blanks between words on any given line in such a way that all lines exactly fit between the predefined left and right margins.  See Program page 56 for examples  Two problem  How do we determine how many words will fit on a single line  Each line may require a different number of blanks to be inserted

31 Application program: text formatter  One possible solution:  Advance through the line of text until a blank is found and then insert a new blank  Then advance again until the end of the next word is found and insert a blank there  Repeat this process until the desired number of blanks have been inserted  See Program page 57 for example

32 Application program: text formatter  Developing the algorithm  See the 5 steps of the algorithm in page 58  The overall process  See the 6 steps of the detailed process of this algorithm in page 58  Four user-defined functions used in this program  initbuff(), get(), loadword(), expand_line() in page 58  See the code for main() in page 58  You are encouraged to think of a different way to achieve the same goal.