Topics discussed in this section:

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Advertisements

Strings.
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 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
 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.
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.
Chapter 9 Formatted Input/Output 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. 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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
© 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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand design concepts for fixed-length and variable- length strings ❏
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
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.
1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able.
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)
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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 differences between text and binary files ❏ To write programs.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Chapter 9 - Formatted Input/Output
Topics Designing a Program Input, Processing, and Output
C Characters and Strings
Strings CSCI 112: Programming in C.
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Chapter 7 Text Input/Output Objectives
Topics discussed in this section:
Chapter 7 Text Input/Output Objectives
Introduction to the C Language
Chapter 9 C Formatted Input/Output
C Characters and Strings
Fundamentals of Characters and Strings
© 2016 Pearson Education, Ltd. All rights reserved.
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
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays Objectives
Chapter 8 - Characters and Strings
Chapter 9 Pointers Objectives
Binary Files.
C Formatted Input / Output Review and Lab Assignments
Object Oriented Programming COP3330 / CGS5409
Chapter 8 Arrays Objectives
Topics discussed in this section:
Chapter 9 - Formatted Input/Output
Topics Designing a Program Input, Processing, and Output
Chapter 8 Arrays Objectives
Topics Designing a Program Input, Processing, and Output
Chapter 9: Pointers and String
C++ Programming Lecture 20 Strings
C Characters and Strings
Topics discussed in this section:
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

FIGURE 11-10 Initializing Strings Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 11-3 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 ). Topics discussed in this section: Formatted String Input/Output String Input/Output Computer Science: A Structured Programming Approach Using C

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

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

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

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

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

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

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

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

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

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

FIGURE 11-11 gets and fgets Functions Computer Science: A Structured Programming Approach Using C

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

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

FIGURE 11-12 puts and fputs Operations Computer Science: A Structured Programming Approach Using C

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

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

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

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

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

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

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

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

11-4 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 C

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

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

FIGURE 11-13 Pointers to Strings Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 11-5 String Manipulation Functions Because a string is not a standard type, we cannot use it directly with most C operators. Fortunately, C provides a set of functions to manipulates strings. Topics discussed in this section: String Length and String Copy String Compare and String Concatenate Character in String Search for a Substring and Search for Character in Set String Span and String Token String to Number Computer Science: A Structured Programming Approach Using C

PROGRAM 11-11 Add Left Margin Computer Science: A Structured Programming Approach Using C

PROGRAM 11-11 Add Left Margin Computer Science: A Structured Programming Approach Using C

FIGURE 11-14 String Copy Computer Science: A Structured Programming Approach Using C

FIGURE 11-15 String-number Copy Computer Science: A Structured Programming Approach Using C

Always use strncpy to copy one string to another. Note Always use strncpy to copy one string to another. Computer Science: A Structured Programming Approach Using C

Build Name Array in Heap PROGRAM 11-12 Build Name Array in Heap Computer Science: A Structured Programming Approach Using C

Build Name Array in Heap PROGRAM 11-12 Build Name Array in Heap Computer Science: A Structured Programming Approach Using C

Build Name Array in Heap PROGRAM 11-12 Build Name Array in Heap Computer Science: A Structured Programming Approach Using C

FIGURE 11-16 Structure for Names Array Computer Science: A Structured Programming Approach Using C

FIGURE 11-17 String Compares Computer Science: A Structured Programming Approach Using C

Results for String Compare Table 11-1 Results for String Compare Computer Science: A Structured Programming Approach Using C

FIGURE 11-18 String Concatenation Computer Science: A Structured Programming Approach Using C

FIGURE 11-19 Character in String (strchr) Computer Science: A Structured Programming Approach Using C

FIGURE 11-20 String in String Computer Science: A Structured Programming Approach Using C

FIGURE 11-21 String Span Computer Science: A Structured Programming Approach Using C

FIGURE 11-22 Streams Computer Science: A Structured Programming Approach Using C

Demonstrate String to Long PROGRAM 11-13 Demonstrate String to Long Computer Science: A Structured Programming Approach Using C

Demonstrate String to Long PROGRAM 11-13 Demonstrate String to Long Computer Science: A Structured Programming Approach Using C

Demonstrate String to Long PROGRAM 11-13 Demonstrate String to Long Computer Science: A Structured Programming Approach Using C

String-to-Number Functions Table 11-2 String-to-Number Functions Computer Science: A Structured Programming Approach Using C

FIGURE 11-23 Parsing with String Token Computer Science: A Structured Programming Approach Using C

Parsing a String with String Token PROGRAM 11-14 Parsing a String with String Token Computer Science: A Structured Programming Approach Using C

Parsing a String with String Token PROGRAM 11-14 Parsing a String with String Token Computer Science: A Structured Programming Approach Using C

Compare Packed String Function PROGRAM 11-15 Compare Packed String Function Computer Science: A Structured Programming Approach Using C

Compare Packed String Function PROGRAM 11-15 Compare Packed String Function Computer Science: A Structured Programming Approach Using C

Compare Packed String Function PROGRAM 11-15 Compare Packed String Function Computer Science: A Structured Programming Approach Using C

Compare Packed String Function PROGRAM 11-15 Compare Packed String Function Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 11-6 String/Data Conversion A common set of applications format data by either converting a sequence of characters into corresponding data types or vice versa. Two such applications are parsing and telecommunications. Topics discussed in this section: String to Data Conversion Data to String Conversion Computer Science: A Structured Programming Approach Using C

FIGURE 11-24 sscanf Operation Computer Science: A Structured Programming Approach Using C

Note sscanf is a one-to-many function. It splits one string into many variables. Computer Science: A Structured Programming Approach Using C

FIGURE 11-25 sprintf Operation Computer Science: A Structured Programming Approach Using C

Demonstrate Memory Formatting PROGRAM 11-16 Demonstrate Memory Formatting Computer Science: A Structured Programming Approach Using C

Demonstrate Memory Formatting PROGRAM 11-16 Demonstrate Memory Formatting Computer Science: A Structured Programming Approach Using C

Testing for Open and Close Errors PROGRAM 11-17 Testing for Open and Close Errors Computer Science: A Structured Programming Approach Using C

Testing for Open and Close Errors PROGRAM 11-17 Testing for Open and Close Errors Computer Science: A Structured Programming Approach Using C

Testing for Open and Close Errors PROGRAM 11-17 Testing for Open and Close Errors Computer Science: A Structured Programming Approach Using C