Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.

Slides:



Advertisements
Similar presentations
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
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.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
Lesson 10 Characters, C-Strings, and the string Class CS1 Lesson John Cole1.
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. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
CS 1400 Chapter 10 Strings. Character testing library #include bool isalpha (char c); bool isalnum (char c); bool isdigit (char c); bool islower (char.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
CS 1400 Apr 18, 2007 Chapter 10 Strings. Character testing library #include bool isalpha (char c); bool isalnum (char c); bool isdigit (char c); bool.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 12: More About.
CS Nov 2006 C-strings.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
C-strings and C++ string Class
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
1 Chapter 10 Characters, Strings, and the string class.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
CHAPTER 8 CHARACTER AND STRINGS
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Lecture 17: Characters, Strings, and the string class Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
 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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
STARTING OUT WITH STARTING OUT WITH Class 9 Honors.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 11 Scott Marino.
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 © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
Characters, Strings, And The string Class Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
Chapter Characters, Strings, and the string class 10.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Characters, Strings, and the cstring Library
C-Strings We have already seen that a C-string is a null-terminated array of characters.
C Characters and Strings
Characters, Strings, and the cstring Library
Characters, C-Strings, and More About the string Class
CSE 303 Lecture 14 Strings in C
Chapter 8 - Characters and Strings
Chapter 12: More on C-Strings and the string Class
Standard Version of Starting Out with C++, 4th Edition
10.1 Character Testing.
CPS120: Introduction to Computer Science
Standard Version of Starting Out with C++, 4th Edition
C Characters and Strings
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class

Chapter 10 slide 2 Topics 10.1 Character Testing 10.2 Character Case Conversion 10.3 Review of the Internal Storage of C- Strings 10.4 Library Functions for Working with C- Strings 10.5 String/Numeric Conversion Functions 10.8 The C++ string Class

Chapter 10 slide Character Testing require cctype header file FUNCTIONMEANING isalpha true if arg. is a letter, false otherwise isalnum true if arg. is a letter or digit, false otherwise isdigit true if arg. is a digit 0-9, false otherwise islower true if arg. is lowercase letter, false otherwise isprint true if arg. is a printable character, false otherwise ispunct true if arg. is a punctuation character, false otherwise isupper true if arg. is an uppercase letter, false otherwise isspacetrue if arg. is a whitespace character, false otherwise

Chapter 10 slide Character Case Conversion require cctype header file functions: toupper : if char argument is lowercase letter, return uppercase equivalent; otherwise, return input unchanged char greeting[] = "Hello!"; char ch; ch= toupper (greeting[1]); cout << ch <<endl; // displays 'E'

Chapter 10 slide 5 Character Case Conversion functions: tolower : if char argument is uppercase letter, return lowercase equivalent; otherwise, return input unchanged char greeting[] = "Hello!"; char ch; ch = tolower (greeting[0]); cout << ch << endl // displays 'h'

Chapter 10 slide Review of the Internal Storage of C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character String literal (string constant): sequence of characters enclosed in double quotes " " : "Hi there!" Hi the re! \0

Chapter 10 slide 7 Review of the Internal Storage of C-Strings Array of char s can be used to define storage for string: char city[20]; Leave room for NULL at end Can enter a value using cin >> –Input is whitespace-terminated –No check to see if enough space For input containing whitespace, and to control amount of input, use cin.getline()

Chapter 10 slide Library Functions for Working with C-Strings require cstring header file functions take one or more C-strings as arguments. Can use: –C-string name –pointer to C-string –literal string

Chapter 10 slide 9 Library Functions for Working with C-Strings Functions: – strlen(str) : returns length of C-string str char city[20] = “Harrisburg"; cout << strlen(city); // prints 10 – strcat(str1, str2) : appends str2 to the end of str1 char location[20] = “Harrisburg, "; char state[3] = “PA"; strcat(location, state); // location now has “Harrisburg, PA"

Chapter 10 slide 10 Library Functions for Working with C-Strings Functions: – strcpy(str1, str2) : copies str2 to str1 char fname[20] = "Maureen", name[20]; strcpy(name, fname); Note: strcat and strcpy perform no bounds checking to determine if there is enough space in receiving character array to hold the string it is being assigned.

Chapter 10 slide 11 C-string Inside a C-string Function: –strstr(str1, str2) : finds the first occurrence of str2 in str1. Returns a pointer to match, or NULL if no match. char city[20] = “Harrisburg"; char word[3] = “is"; cout << strstr(city, word); // displays “isburg"

Chapter 10 slide String/Numeric Conversion Functions require cstdlib header file FUNCTIONPARAMETERACTION atoi C-string converts C-string to an int value, returns the value atol C-string converts C-string to a long value, returns the value atof C-string converts C-string to a double value, returns the value itoaint, C-string, int converts 1 st int parameter to a C- string, stores it in 2 nd parameter. 3 rd parameter is base of converted value

Chapter 10 slide 13 String/Numeric Conversion Functions int iNum; long lNum; float fNum; char intChar[10]; iNum = atoi("1234"); // puts 1234 in iNum lNum = atol("5678"); // puts 5678 in lNum fNum = atof("35.7"); // puts 35.7 in fNum itoa(iNum, intChar, 8); // puts the string // "2322" (base 8 for ) in intChar

Chapter 10 slide 14 String/Numeric Conversion Functions - Notes if C-string contains non-digits, results are undefined –function may return result up to non-digit –function may return 0 itoa does no bounds checking – make sure there is enough space to store the result

Chapter 10 slide The C++ string Class Special datatype supports working with strings #include Can define string variables in programs: string firstName, lastName; Can receive values with assignment operator: firstName = "George"; lastName = "Washington"; Can be displayed via cout cout << firstName << " " << lastName;

Chapter 10 slide 16 Input into a string Object Use getline function to put a line of input, possibly including spaces, into a string: string address; cout << "Enter your address: "; getline(cin,address);

Chapter 10 slide 17 string Comparison Can use relational operators directly to compare string objects: string str1 = "George", str2 = "Georgia"; if (str1 < str2) cout << str1 << " is less than " << str2; Comparison is performed similar to strcmp function. Result is true or false

Chapter 10 slide 18 Other Definitions of C++ string s DefinitionMeaning string name; defines an empty string object string myname("Chris"); defines a string and initializes it string yourname(myname); defines a string and initializes it string aname(myname, 3); defines a string and initializes it with first 3 characters of myname string verb(myname,3,2); defines a string and initializes it with 2 characters from myname starting at position 3 string noname('A', 5); defines string and initializes it to 5 'A' s

Chapter 10 slide 19 string Operators OPERATORMEANING >> extracts characters from stream up to whitespace, insert into string << inserts string into stream = assigns string on right to string object on left += appends string on right to end of contents on left + concatenates two strings [] references character in string using array notation >, >=, <, <=, ==, != relational operators for string comparison. Return true or false

Chapter 10 slide 20 string Operators string word1, phrase; string word2 = " Dog"; cin >> word1; // user enters "Hot Tamale" // word1 has "Hot" phrase = word1 + word2; // phrase has // "Hot Dog" phrase += " on a bun"; for (int i = 0; i < 16; i++) cout << phrase[i]; // displays // "Hot Dog on a bun"

Chapter 10 slide 21 string Member Functions Are behind many overloaded operators Categories: –assignment: assign, copy, data –modification: append, clear, erase, insert, replace, swap –space management: capacity, empty, length, resize, size –substrings: find, substr –comparison: compare

Chapter 10 slide 22 string Member Functions string word1, word2, phrase; cin >> word1; // word1 has "Hot" word2.assign(" Dog"); phrase.append(word1); phrase.append(word2); // phrase has "Hot Dog" phrase.append(" with mustard relish", 13); // phrase has "Hot Dog with mustard" phrase.insert(8, "on a bun "); cout << phrase << endl; // displays // "Hot Dog on a bun with mustard"

Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class