© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.

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.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
 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.
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 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 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.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
 2006 Pearson Education, Inc. All rights reserved Bits, Characters, C-Strings and struct s.
 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. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 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.
Programming Languages -1 (Introduction to C) strings Instructor: M.Fatih AMASYALI
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.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
 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.
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’};
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
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.
Characters, Strings, And The string Class Chapter 10.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
© 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. 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.
Characters and Strings
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
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
C Characters and Strings
Fundamentals of Characters and Strings
Characters and Strings
Programming Languages -1 (Introduction to C) strings
CSE 303 Lecture 14 Strings in C
Chapter 8 - Characters and Strings
Pointers and Pointer-Based Strings
C Characters and Strings – Review Lab assignments
C++ Programming Lecture 20 Strings
Lecture 19: Working with strings
Characters and Strings Functions
Characters and Strings
C Characters and Strings
Presentation transcript:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings 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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 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.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction 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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals 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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters 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'

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Character 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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Character Handling Library

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 fig08_02.c (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 fig08_02.c (Part 2 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 Program Output According to isdigit: 8 is a digit # is not a digit According to isalpha: A is a letter b is a letter & is not a letter 4 is not a letter According to isalnum: A is a digit or a letter 8 is a digit or a letter # is not a digit or a letter According to isxdigit: F is a hexadecimal digit J is not a hexadecimal digit 7 is a hexadecimal digit $ is not a hexadecimal digit f is a hexadecimal digit

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 fig08_03.c (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 fig08_03.c (Part 2 of 2) Program Output According to islower: p is a lowercase letter P is not a lowercase letter 5 is not a lowercase letter ! is not a lowercase letter According to isupper: D is an uppercase letter d is not an uppercase letter 8 is not an uppercase letter $ is not an uppercase letter u converted to uppercase is U 7 converted to uppercase is 7 $ converted to uppercase is $ L converted to lowercase is l

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 fig08_04.c (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 fig08_04.c (Part 2 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 Program Output According to isspace: Newline is a whitespace character Horizontal tab is a whitespace character % is not a whitespace character According to iscntrl: Newline is a control character $ is not a control character According to ispunct: ; is a punctuation character Y is not a punctuation character # is a punctuation character According to isprint: $ is a printing character Alert is not a printing character According to isgraph: Q is a printing character other than a space Space is not a printing character other than a space

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved String Conversion Functions Conversion functions –In (general utilities library) Convert strings of digits to integer and floating- point values

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 fig 08_06.c Program Output The string "99.0" converted to double is The converted value divided by 2 is

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 fig08_07.c Program Output The string "2593" converted to int is 2593 The converted value minus 593 is 2000

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 fig08_08.c Program Output The string " " converted to long int is The converted value divided by 2 is

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 fig08_09.c Program Output The string "51.2% are admitted" is converted to the double value and the string "% are admitted"

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21 fig08_10.c Program Output The original string is " abc" The converted value is The remainder of the original string is "abc" The converted value plus 567 is

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 fig08_11.c Program Output The original string is " abc" The converted value is The remainder of the original string is "abc" The converted value minus 567 is

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Standard Input/Output Library Functions Functions in Used to manipulate character and string data

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 fig08_13.c (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 fig08_13.c (Part 1 of 2) Program Output Enter a line of text: Characters and Strings The line printed backwards is: sgnirtS dna sretcarahC Enter a line of text: able was I ere I saw elba The line printed backwards is: able was I ere I saw elba

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 fig8_14.c

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27 Program Output Enter a line of text: This is a test. The line entered was: This is a test.

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 28 fig08_15.c Program Output Enter an integer and a double: The formatted output stored in array s is: integer: 298 double: 87.38

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 fig08_16.c Program Output The values stored in character array s are: integer: double:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved String Manipulation Functions of the String Handling Library String handling library has functions to –Manipulate string data –Search strings –Tokenize strings –Determine string length

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 31 fig08_18.c Program Output The string in array x is: Happy Birthday to You The string in array y is: Happy Birthday to You The string in array z is: Happy Birthday

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 32 fig08_19.c

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 33 Program Output s1 = Happy s2 = New Year strcat( s1, s2 ) = Happy New Year strncat( s3, s1, 6 ) = Happy strcat( s3, s1 ) = Happy Happy New Year

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Comparison 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

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 35 fig08_21.c

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 36 Program Output s1 = Happy New Year s2 = Happy New Year s3 = Happy Holidays strcmp(s1, s2) = 0 strcmp(s1, s3) = 1 strcmp(s3, s1) = -1 strncmp(s1, s3, 6) = 0 strncmp(s1, s3, 7) = 1 strncmp(s3, s1, 7) = -1

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Search Functions of the String Handling Library

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 38 fig08_23.c (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 39 fig08_23.c (Part 2 of 2) Program Output 'a' was found in "This is a test". 'z' was not found in "This is a test".

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 40 fig08_24.c Program Output string1 = The value is string2 = The length of the initial segment of string1 containing no characters from string2 = 13

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 41 fig08_25.c Program Output Of the characters in "beware" 'a' is the first character to appear in "This is a test"

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 42 fig08_26.c Program Output The remainder of string1 beginning with the last occurrence of character 'z' is: "zebras"

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 43 fig08_27.c Program Output string1 = The value is string2 = aehi lsTuv The length of the initial segment of string1 containing only characters from string2 = 13

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 44 fig08_28.c Program Output string1 = abcdefabcdef string2 = def The remainder of string1 beginning with the first occurrence of string2 is: defabcdef

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 45 fig08_29.c

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 46 Program Output The string to be tokenized is: This is a sentence with 7 tokens The tokens are: This is a sentence with 7 tokens

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Memory Functions of the String- handling Library

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 49 fig08_31.c Program Output After s2 is copied into s1 with memcpy, s1 contains "Copy this string"

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 50 fig08_32.c Program Output The string in array x before memmove is: Home Sweet Home The string in array x after memmove is: Sweet Home Home

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 51 fig08_33.c Program Output s1 = ABCDEFG s2 = ABCDXYZ memcmp( s1, s2, 4 ) = 0 memcmp( s1, s2, 7 ) = -1 memcmp( s2, s1, 7 ) = 1

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 52 Fig8_34.c Program Output The remainder of s after character 'r' is found is "ring“

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Other 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

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 54 fig08_35.c Program Output string1 = BBBBBBBBBBBBBB string1 after memset = bbbbbbbBBBBBBB

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 55 fig08_37.c Program Output No such file or directory

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 56 fig08_38.c Program Output The length of "abcdefghijklmnopqrstuvwxyz" is 26 The length of "four" is 4 The length of "Boston" is 6