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).

Slides:



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

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.
 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.
Week 8 Arrays Part 2 String & Pointer
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
1. An array is a collection of a fixed number of components wherein all of the components are of the same type Example: Suppose that there is a list of.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
 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.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
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.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
© 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:
 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.
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.
Characters and Strings
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,
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
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
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
Fundamentals of Characters and Strings
Characters and Strings
Chapter 8 - Characters and Strings
Pointers and Pointer-Based Strings
C Characters and Strings – Review Lab assignments
String in C++.
C++ Programming Lecture 20 Strings
Lecture 19: Working with strings
Characters and Strings Functions
Characters and Strings
C Characters and Strings
Presentation transcript:

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). –To be able to use the string and character input/output functions of the standard input/output library ( stdio). –To be able to use the string conversion functions of the general utilities library ( stdlib). –To be able to use the string processing functions of the string handling library ( string). –To appreciate the power of function libraries as a means of achieving software reusability.

Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character Handling Library 8.4String Conversion Functions 8.5Standard Input/Output Library Functions 8.6String Manipulation Functions of the String Handling Library 8.7Comparison Functions of the String Handling Library 8.8Search Functions of the String Handling Library 8.9Memory Functions of the String Handling Library 8.10Other Functions of the String Handling Library

8.1Introduction Introduce some standard library functions –Easy string and character processing –Programs can process characters, strings, lines of text, and blocks of memory These techniques used to make –Word processors –Page layout software –Typesetting programs

8.2Fundamentals of Strings and Characters Characters –Building blocks of programs Every program is a sequence of meaningfully grouped characters –Character constant An int value represented as a character in single quotes 'z' represents the integer value of z Strings –Series of characters treated as a single unit Can include letters, digits and special characters (*, /, $) –String literal (string constant) - written in double quotes "Hello" –Strings are arrays of characters String a pointer to first character Value of string is the address of first character

8.2Fundamentals of Strings and Characters (cont) String definitions –Define as a character array or a variable of type char * char color[] = "blue"; char *colorPtr = "blue"; –Remember that strings represented as character arrays end with '\0' color has 5 elements Inputting strings –Use scanf scanf("%s", word); Copies input into word[] Do not need & (because a string is a pointer) –Remember to leave room in the array for '\0'

8.3Character Handling Library Character handling library –Includes functions to perform useful tests and manipulations of character data –Each function receives a character (an int ) or EOF as an argument The following slide contains a table of all the functions in

8.3 Character Handling Library (cont)

Fig08_02.c Fig08_03.c Fig08_04.c

8.4 String Conversion Functions Conversion functions –In (general utilities library) Convert strings of digits to integer and floating- point values

8.4 String Conversion Functions Fig08_06.c Fig08_07.c Fig08_08.c Fig08_09.c Fig08_10.c Fig08_11.c

8.5Standard Input/Output Library Functions Functions in Used to manipulate character and string data

8.5Standard Input/Output Library Functions Fig08_13.c Fig08_14.c Fig08_15.c Fig08_16.c

8.6String Manipulation Functions of the String Handling Library String handling library has functions to –Manipulate string data –Search strings –Tokenize strings –Determine string length

Fig08_18.c Fig08_19.c

8.7Comparison Functions of the String Handling Library Comparing strings –Computer compares numeric ASCII codes of characters in string –Appendix D has a list of character codes int strcmp( const char *s1, const char *s2 ); –Compares string s1 to s2 –Returns a negative number if s1 s2 int strncmp( const char *s1, const char *s2, size_t n ); –Compares up to n characters of string s1 to s2 –Returns values as above

Fig08_21.c

8.8Search Functions of the String Handling Library

Fig08_23.c Fig08_24.c Fig08_25.c Fig08_26.c Fig08_27.c Fig08_28.c Fig08_29.c

8.9 Memory Functions of the String- handling Library Memory Functions –In –Manipulate, compare, and search blocks of memory –Can manipulate any block of data Pointer parameters are void * –Any pointer can be assigned to void *, and vice versa –void * cannot be dereferenced Each function receives a size argument specifying the number of bytes (characters) to process

8.9 Memory Functions of the String- handling Library

Fig08_31.c Fig08_32.c Fig08_33.c Fig08_34.c

8.10Other Functions of the String Handling Library char *strerror( int errornum ); –Creates a system-dependent error message based on errornum –Returns a pointer to the string size_t strlen( const char *s ); –Returns the number of characters (before NULL ) in string s

Fig08_35.c Fig08_37.c Fig08_38.c