C How to Program, 6/e ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

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

EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
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.
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.
 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.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Types and Variables. Computer Programming 2 C++ in one page!
 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.
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
 2006 Pearson Education, Inc. All rights reserved Bits, Characters, C-Strings and struct s.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
 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.
Chapter 2: C Fundamentals Dr. Ameer Ali. Overview C Character set Identifiers and Keywords Data Types Constants Variables and Arrays Declarations Expressions.
 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.
CHAPTER 8 CHARACTER AND STRINGS
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
 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.
(continue) © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Chapter 8: Character and String CMPD144: Programming 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
 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. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
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).
UniMAP SEM I - 10/11EKT 120 Computer Programming1 Lecture 8 – Arrays (2) and Strings.
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.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Characters and Strings
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
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,
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
C Characters and Strings
C Characters and Strings
Fundamentals of Characters and Strings
JavaScript Objects.
Chapter 8 - Characters and Strings
Pointers and Pointer-Based Strings
C++ Programming Lecture 20 Strings
Lecture 19: Working with strings
Characters and Strings Functions
C Characters and Strings
Presentation transcript:

C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.

 In this chapter, we introduce the C Standard Library functions that facilitate string and character processing.  The functions enable programs to process characters, strings, lines of text and blocks of memory.  The chapter discusses the techniques used to develop editors, word processors, page layout software, computerized typesetting systems and other kinds of text- processing software.  The text manipulations performed by formatted input/output functions like printf and scanf can be implemented using the functions discussed in this chapter. © by Pearson Education, Inc. All Rights Reserved.

 Characters are the fundamental building blocks of source programs.  Every program is composed of a sequence of characters that—when grouped together meaningfully—is interpreted by the computer as a series of instructions used to accomplish a task.  A program may contain character constants.  A character constant is an int value represented as a character in single quotes.  The value of a character constant is the integer value of the character in the machine’s character set. © by Pearson Education, Inc. All Rights Reserved.

 For example, 'z' represents the integer value of z, and '\n' the integer value of newline (122 and 10 in ASCII, respectively).  A string is a series of characters treated as a single unit.  A string may include letters, digits and various special characters such as +, -, *, / and $.  String literals, or string constants, in C are written in double quotation marks. © by Pearson Education, Inc. All Rights Reserved.

 A string in C is an array of characters ending in the null character ( '\0' ).  A string is accessed via a pointer to the first character in the string.  The value of a string is the address of its first character.  Thus, in C, it is appropriate to say that a string is a pointer—in fact, a pointer to the string’s first character.  In this sense, strings are like arrays, because an array is also a pointer to its first element.  A character array or a variable of type char * can be initialized with a string in a definition. © by Pearson Education, Inc. All Rights Reserved.

 The definitions  char color[] = "blue"; const char *colorPtr = "blue"; each initialize a variable to the string "blue".  The first definition creates a 5-element array color containing the characters 'b', 'l', 'u', 'e' and '\0'.  The second definition creates pointer variable colorPtr that points to the string "blue" somewhere in memory. © by Pearson Education, Inc. All Rights Reserved.

 The preceding array definition could also have been written  char color[] = { 'b', 'l', 'u', 'e', '\0' };  When defining a character array to contain a string, the array must be large enough to store the string and its terminating null character.  The preceding definition automatically determines the size of the array based on the number of initializers in the initializer list. © by Pearson Education, Inc. All Rights Reserved.

 The character-handling library ( ) includes several functions that perform useful tests and manipulations of character data.  Each function receives a character—represented as an int —or EOF as an argument.  EOF normally has the value –1, and some hardware architectures do not allow negative values to be stored in char variables, so the character-handling functions manipulate characters as integers.  Figure 8.1 summarizes the functions of the character- handling library. © by Pearson Education, Inc. All Rights Reserved.

 Figure 8.4 demonstrates functions isspace, iscntrl, ispunct, isprint and isgraph.  Function isspace determines if a character is one of the following white-space characters: space ( ' ' ), form feed ( '\f' ), newline ( '\n' ), carriage return ( '\r' ), horizontal tab ( '\t' ) or vertical tab ( '\v' ). © by Pearson Education, Inc. All Rights Reserved.

 Function iscntrl determines if a character is one of the following control characters: horizontal tab ( '\t' ), vertical tab ( '\v' ), form feed ( '\f' ), alert ( '\a' ), backspace ( '\b' ), carriage return ( '\r' ) or newline ( '\n' ).  Function ispunct determines if a character is a printing character other than a space, a digit or a letter, such as $, #, (, ), [, ], {, }, ;, : or %.  Function isprint determines if a character can be displayed on the screen (including the space character).  Function isgraph is the same as isprint, except that the space character is not included. © by Pearson Education, Inc. All Rights Reserved.

 This section presents the string-conversion functions from the general utilities library ( ).  These functions convert strings of digits to integer and floating-point values.  Figure 8.5 summarizes the string-conversion functions.  Note the use of const to declare variable nPtr in the function headers (read from right to left as “ nPtr is a pointer to a character constant”); const specifies that the argument value will not be modified. © by Pearson Education, Inc. All Rights Reserved.

 This section presents several functions from the standard input/output library ( ) specifically for manipulating character and string data.  Figure 8.12 summarizes the character and string input/output functions of the standard input/output library. © by Pearson Education, Inc. All Rights Reserved.

 The string-handling library ( ) provides many useful functions for manipulating string data (copying strings and concatenating strings), comparing strings, searching strings for characters and other strings, tokenizing strings (separating strings into logical pieces) and determining the length of strings.  This section presents the string-manipulation functions of the string-handling library.  The functions are summarized in Fig  Every function—except for strncpy —appends the null character to its result. © by Pearson Education, Inc. All Rights Reserved.

 Functions strncpy and strncat specify a parameter of type size_t, which is a type defined by the C standard as the integral type of the value returned by operator sizeof. © by Pearson Education, Inc. All Rights Reserved.

 This section presents the string-handling library’s string-comparison functions, strcmp and strncmp.  Fig contains their prototypes and a brief description of each function. © by Pearson Education, Inc. All Rights Reserved.

 In an effort to standardize character representations, most computer manufacturers have designed their machines to utilize one of two popular coding schemes—ASCII or EBCDIC.  ASCII stands for “American Standard Code for Information Interchange,” and EBCDIC stands for “Extended Binary Coded Decimal Interchange Code.”  There are other coding schemes, but these two are the most popular.  The Unicode ® Standard outlines a specification to produce consistent encoding of the vast majority of the world’s characters and symbols. © by Pearson Education, Inc. All Rights Reserved.

 To learn more about Unicode, visit  ASCII, EBCDIC and Unicode are called character sets.  String and character manipulations actually involve the manipulation of the appropriate numeric codes and not the characters themselves.  This explains the interchangeability of characters and small integers in C.  Since it is meaningful to say that one numeric code is greater than, less than or equal to another numeric code, it becomes possible to relate various characters or strings to one another by referring to the character codes.  Appendix B lists the ASCII character codes. © by Pearson Education, Inc. All Rights Reserved.

 This section presents the functions of the string- handling library used to search strings for characters and other strings.  The functions are summarized in Fig  The functions strcspn and strspn return size_t. © by Pearson Education, Inc. All Rights Reserved.

 The string-handling library functions presented in this section manipulate, compare and search blocks of memory.  The functions treat blocks of memory as character arrays and can manipulate any block of data.  Figure 8.30 summarizes the memory functions of the string-handling library.  In the function discussions, “object” refers to a block of data. © by Pearson Education, Inc. All Rights Reserved.

 The two remaining functions of the string-handling library are strerror and strlen.  Figure 8.36 summarizes the strerror and strlen functions. © by Pearson Education, Inc. All Rights Reserved.