Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand design concepts for fixed-length and variable- length strings ❏

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
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.
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.
 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.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
© 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.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Week 7 – String. Outline Passing Array to Function Print the Array How Arrays are passed in a function call Introduction to Strings String Type Character.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 8: Arrays.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
Chapter 7 Formatted input and output. 7.1 introduction Tax: This result is correct; but it would be better Maybe as $13, Make formatting.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the structure, union, and enumerated types ❏ To use the type definition.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 STRINGS String data type Basic operations on strings String functions String procedures.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
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)
Characters and Strings
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 12 Enumerated, Structure, and Union Types Objectives
Introduction to the C Language
Fundamentals of Characters and Strings
String Concepts In general, a string is a series of characters treated as a unit. Computer science has long recognized the importance of strings, but it.
Chapter 7 Text Input/Output Objectives
Scope, Objects, Strings, Numbers
Chapter 8 - Characters and Strings
Topics discussed in this section:
Chapter 9 Pointers Objectives
Introduction to the C Language
Introduction to the C Language
Topics discussed in this section:
Topics discussed in this section:
Topics discussed in this section:
Chapter 9: Pointers and String
C++ Programming Lecture 20 Strings
Presentation transcript:

Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand design concepts for fixed-length and variable- length strings ❏ To understand the design implementation for C-language delimited strings ❏ To write programs that read, write, and manipulate strings ❏ To write programs that use the string functions ❏ To write programs that use arrays of strings ❏ To write programs that parse a string into separate variables ❏ To understand the software engineering concepts of information hiding and cohesion Chapter 11 Chapter 11 Strings Strings

Computer Science: A Structured Programming Approach Using C String Concepts In general, a string is a series of characters treated as a unit. Computer science has long recognized the importance of strings, but it has not adapted a standard for their implementation. We find, therefore, that a string created in Pascal differs from a string created in C. Fixed-Length Strings Variable-Length Strings Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C3 FIGURE 11-1 String Taxonomy

Computer Science: A Structured Programming Approach Using C4 FIGURE 11-2 String Formats

Computer Science: A Structured Programming Approach Using C C Strings A C string is a variable-length array of characters that is delimited by the null character. Storing Strings The String Delimiter String Literals Strings and Characters Declaring Strings Initializing Strings Strings and the Assignment Operator Reading and Writing Strings Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C6 C uses variable-length, delimited strings. Note

Computer Science: A Structured Programming Approach Using C7 FIGURE 11-3 Storing Strings

Computer Science: A Structured Programming Approach Using C8 FIGURE 11-4 Storing Strings and Characters

Computer Science: A Structured Programming Approach Using C9 FIGURE 11-5 Differences Between Strings and Character Arrays

Computer Science: A Structured Programming Approach Using C10 FIGURE 11-6 Strings in Arrays

Computer Science: A Structured Programming Approach Using C11 A string literal is enclosed in double quotes. Note

Computer Science: A Structured Programming Approach Using C12 FIGURE 11-7 Character Literals and String Literals

Computer Science: A Structured Programming Approach Using C13 FIGURE 11-8 String Literal References

Computer Science: A Structured Programming Approach Using C14 FIGURE 11-9 Defining Strings

Computer Science: A Structured Programming Approach Using C15 Memory for strings must be allocated before the string can be used. Note

Computer Science: A Structured Programming Approach Using C16 FIGURE Initializing Strings

Computer Science: A Structured Programming Approach Using C String Input/Output Functions C provides two basic ways to read and write strings. First, we can read and write strings with the formatted input/output functions, scanf/fscanf and printf/fprintf. Second, we can use a special set of string-only functions, get string (gets/fgets) and put string ( puts/fputs ). Formatted String Input/Output String Input/Output Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C18 The string conversion code(s) skips whitespace. Note

Computer Science: A Structured Programming Approach Using C19 PROGRAM 11-1Reading Strings

Computer Science: A Structured Programming Approach Using C20 The edit set does not skip whitespace. Note

Computer Science: A Structured Programming Approach Using C21 Always use a width in the field specification when reading strings. Note

Computer Science: A Structured Programming Approach Using C22 The maximum number of characters to be printed is specified by the precision in the format string of the field specification. Note

Computer Science: A Structured Programming Approach Using C23 PROGRAM 11-2Demonstrate String Scan Set

Computer Science: A Structured Programming Approach Using C24 PROGRAM 11-2Demonstrate String Scan Set

Computer Science: A Structured Programming Approach Using C25 PROGRAM 11-3Delete Leading Whitespace

Computer Science: A Structured Programming Approach Using C26 PROGRAM 11-3Delete Leading Whitespace

Computer Science: A Structured Programming Approach Using C27 PROGRAM 11-4Read Student Names and Scores

Computer Science: A Structured Programming Approach Using C28 PROGRAM 11-4Read Student Names and Scores

Computer Science: A Structured Programming Approach Using C29 FIGURE gets and fgets Functions

Computer Science: A Structured Programming Approach Using C30 PROGRAM 11-5Demonstrate fgets Operation

Computer Science: A Structured Programming Approach Using C31 PROGRAM 11-5Demonstrate fgets Operation

Computer Science: A Structured Programming Approach Using C32 FIGURE puts and fputs Operations

Computer Science: A Structured Programming Approach Using C33 PROGRAM 11-6Demonstration of Put String

Computer Science: A Structured Programming Approach Using C34 PROGRAM 11-6Demonstration of Put String

Computer Science: A Structured Programming Approach Using C35 PROGRAM 11-7Typewriter Program

Computer Science: A Structured Programming Approach Using C36 PROGRAM 11-7Typewriter Program

Computer Science: A Structured Programming Approach Using C37 PROGRAM 11-8Print Selected Sentences

Computer Science: A Structured Programming Approach Using C38 PROGRAM 11-8Print Selected Sentences

Computer Science: A Structured Programming Approach Using C39 PROGRAM 11-9Print File Double spaced

Computer Science: A Structured Programming Approach Using C40 PROGRAM 11-9Print File Double spaced

Computer Science: A Structured Programming Approach Using C Arrays of Strings When we discussed arrays of pointers in Chapter 10, we introduced the concept of a ragged array. Ragged arrays are very common with strings. Consider, for example, the need to store the days of the week in their textual format. We could create a two-dimensional array of seven days by ten characters, but this wastes space.

Computer Science: A Structured Programming Approach Using C42 PROGRAM 11-10Print Days of the Week

Computer Science: A Structured Programming Approach Using C43 PROGRAM 11-10Print Days of the Week

Computer Science: A Structured Programming Approach Using C44 FIGURE Pointers to Strings