Chapter 9 Strings J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology.

Slides:



Advertisements
Similar presentations
Problem Solving & Program Design in C
Advertisements

Introduction to C Programming
 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.
Lecture 20 Arrays and Strings
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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
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.
Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
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.
Chapter 10.
Topic 10 - Strings.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 CS 201 String Debzani Deb. 2 Distinction Between Characters and Strings When using strcat, one may be tempted to supply a single character as one of.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Strings String - a string is a series of characters treated as a unit. A C string is a variable-length array of characters that is delimited by the null.
CS 201 String Debzani Deb.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
Chapter 8 Arrays and Strings
Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
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’};
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
1 This chapter covers both string constants (or literals, as they're called in the C standard) and string variables, which can change during the execution.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
Chapter 11 Structure and Union Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
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.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
chap9 Chapter 9 Strings chap9 2 Strings A data structure deals with a grouping of characters. C implements the string data structure.
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Today’s Material Strings Definition Representation Initialization
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
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.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Problem Solving and Program Design in C Chap. 8 Strings Chow-Sing Lin.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
Strings CSCI 112: Programming in C.
Strings (Continued) Chapter 13
© 2016 Pearson Education, Ltd. All rights reserved.
A First Book of ANSI C Fourth Edition
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
Chapter 8 Character Arrays and Strings
Chapter 9: Strings Problem Solving and Program Design in C 5th Edition
Computer Science II CS132/601* Lecture #C-3.
Introduction to Problem Solving and Programming
Presentation transcript:

Chapter 9 Strings J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 String a grouping of characters use arrays of type char

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-3 String Basics #define ERR_PREFIX "*****Error - " #define INSUFF_DATA "Insufficient Data" char string_var[30]; initialization of string variables using either a brace- enclosed character list, or a string constant –char str[20] = "Initial value"; null character –character '\0' that marks the end of a string in C

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-4 Arrays of Strings an array of strings is a two-dimensional array of characters in which each row is one string. –#define NUM_PEOPLE 30 –#define NAME_LEN 25 –... –char names[NUM_PEOPLE][NAME_LEN]; Initialization –char month[12][10] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-5 Input/Output with printf and scanf right-justified –printf ("***%8s***%3s***\n", "Short", "Strings"); left justified –printf("%-20s\n", president);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-6 Input/Output with printf and scanf (Cont’d) Do not apply the address-of operator to a string argument passed to scanf The approach scanf takes to string input is very similar to its processing of numeric input. –scanf skips leading whitespace characters such as blanks, newlines, and tabs

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-7

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-8 Input/Output with printf and scanf (Cont’d) When it comes across a whitespace character, scanning stops, and scanf places the null character at the end of the string in its array argument.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-9 Input/Output with printf and scanf (Cont’d) scanf would have difficulty if some essential whitespace between values were omitted or if a nonwhitespace separator were substituted. For Fig. 9.2: –MATH 1270 TR 1800 Even more… out of space problem! –MATH,1270,TR,1800

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-10 Example int ages[NUM_PEOPLE];

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-11 String Library Functions: Assignment and Substrings A declaration of a string variable with initialization is the only place in which the operator means to copy the string. An array name with no subscript represents the address of the initial array element, which is constant and cannot be changed through assignment. Invalid target of assignment: –char one_str[20]; –one_str = "Test string"; /* Does not work */

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-12

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-13 String Assignment char one_str[20] strcpy(one_str, "Test String"); Overflow: –strcpy(one_str, "A very long test string");

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-14 Limited size copying: strncpy strncpy(one_str, "Test string", 20); If the string to be copied (the source string) is shorter than n characters, the remaining characters stored are null.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-15 Limited size copying: strncpy when the source string is longer than n characters, only the first n characters are copied. strncpy(one_str, "A very long test string", 20); no terminating '\0‘ ! Solution –strncpy(dest, source, dest_len - 1); –dest[dest_len - 1] = '\0';

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-16 Substrings Substring a fragment of a longer string Example –char result[10], s1[15] = "Jan. 30, 1996"; –strncpy(result, s1, 9); –result[9] = '\0'; –strncpy(result, &s1[5], 2); –result[2] = '\0';

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-17 Substrings (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-18 Substrings (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-19 Substrings (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-20 Substrings (Cont’d) string length –in a character array, the number of characters before the first null character –strlen empty string –a string of length zero: the first character of the string is the null character

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-21 Concatenation –joining of two strings strcat, strncat Example #define STRSIZ 15 char f1[STRSIZ] = "John ", f2[STRSIZ] = "Jacqueline ", last[STRSIZ] = "Kennedy"; strcat(f1, last); strcat(f2, last); /* invalid overflow of f2 */ strncat(f2, last, 3);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-22 Concatenation (Cont’d) Example of overflow prevention if (strlen(s1) + strlen(s2) < STRSIZ) { strcat(s1, s2); } else { strncat(s1, s2, STRSIZ - strlen(s1) - 1); s1[STRSIZ - 1] = '\0'; } Always be in the mind –Is there enough space in the output parameter? –Does the created string end in '\0'?

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-23 Characters and Strings character 'Q' and the string "Q"

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-24 Scanning a Full Line gets –input of one complete line of data –Example char line[80]; printf("Type in a line of data.\n"> "); gets(line); Input: > Here is a short sentence \n is not stored

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-25 Scanning a Full Line (Cont’d) fgets takes three arguments — the output parameter string, a maximum number of characters to store (n), and the file pointer to the data source. fgets will never store more than n -1 characters from the data file. The final character stored will always be '\0'. If fgets has room to store the entire line of data, it will include '\n' before '\0'. –If the line is truncated, no '\n' is stored. When a call to fgets encounters the end of file, the value returned is the address 0, which is considered the null pointer.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-26

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-27

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-28 String Comparison str1 < str2 is not checking whether str1 precedes str2 alphabetically. function strcmp If the first n characters of str1 and str2 match and str1[n], str2[n] are the first nonmatching corresponding characters, str1 is less than str2 if str1[n] < str2[n].

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-29 String Comparison (Cont’d) If str1 is shorter than str2 and all the characters of str1 match the corresponding characters of str2, str1 is less than str2. –Str1: j o y –Str2: j o y o u s

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-30 String Comparison (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-31 Arrays of Pointers

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-32 Arrays of Pointers (Cont’d) char *alphap[5]; Let –alphap[0] address of "daisy" –alphap[1] address of "marigold" –alphap[2] address of "petunia" –alphap[3] address of "rose" –alphap[4] address of "tulip" for (i = 0; i < 5; ++i) printf("%s\n", alphap[i]);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-33 Arrays of Pointers (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-34 Figure 9.14 Two Orderings of One List Using an Array of Pointers

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-35 Figure 9.14 Two Orderings of One List Using an Array of Pointers (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-36 Figure 9.14 Two Orderings of One List Using an Array of Pointers (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-37 Arrays of Pointers (Cont’d) several benefits –A pointer requires less storage space than a full copy of a character string. –The sorting function executes faster when it copies only pointers and not complete arrays of characters.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-38 Arrays of String Constants Compare: –char month[12][10] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; –char *month[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-39 Character Operations input –scanf("%c", &ch) –ch = getchar() –ch = getc(infilep) output –putchar('a'); –putc('a', outp);

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-40 Figure 9.15 Implementation of scanline Function Using getchar

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-41 Character Analysis and Conversion

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-42 Figure 9.16 String Function for a Greater- Than Operator That Ignores Case

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-43 Figure 9.16 String Function for a Greater- Than Operator That Ignores Case (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-44 String-to-Number and Number-to-String Conversions sprintf sscanf

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-45 String-to-Number and Number-to-String Conversions (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-46 String-to-Number and Number-to-String Conversions (Cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-47 Figure 9.18 Functions That Convert Representations of Dates

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-48 Figure 9.18 Functions That Convert Representations of Dates (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-49 Figure 9.18 Functions That Convert Representations of Dates (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-50 Figure 9.18 Functions That Convert Representations of Dates (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-51 Common Programming Errors Returning a string value in a function returns the address of the string in a local variable. –The problem with this approach is that the function’s data area is deallocated as soon as the return statement is executed, so it is not valid to access from the calling module the string the function constructed in its own local variable.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-52 Figure 9.22 Flawed scanline Returns Address of Deallocated Space

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-53 Common Programming Errors (Cont’d) Misuse or neglect of the & operator to strings or to any other whole arrays Overflow of character arrays allocated for strings –Requiring an input argument telling how much space is available for the string result prevent storage of a string that is too long. Strings must end with the null character. Do not use equality or relational operators when comparing strings or the assignment operator for copying them. –Use strcmp or strncmp for comparisons –Use strcpy or strncpy for copying strings.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-54 Figure 9.19 Structure Chart for Text Editor Program

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-55 Figure 9.20 Text Editor Program

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-56 Figure 9.20 Text Editor Program (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-57

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-58 Figure 9.20 Text Editor Program (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-59 Figure 9.20 Text Editor Program (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-60 Figure 9.20 Text Editor Program (cont’d)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-61 Figure 9.21 Sample Run of Text Editor Program

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-62 Figure 9.23 Bands Encoding the Resistance Value of a Resistor