Chapter 10 Characters, Strings, and the string class

Similar documents
Chapter 10: Character Testing. From Program Character Case Conversion 8/23/2014. Character Testing. Character Case Conversion

Chapter 10: Characters, C- Strings, and More About the string Class

Chapter 10: Characters, C- Strings, and More About the string Class Character Testing

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

System Design and Programming II

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

Chapter 8 - Characters and Strings

CSCE 110 PROGRAMMING FUNDAMENTALS

Fundamentals of Programming. Lecture 11: C Characters and Strings

Engineering Problem Solving with C++, Etter

C: How to Program. Week /May/28

LECTURE 15. String I/O and cstring library

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

Strings. Upsorn Praphamontripong. Note: for reference when we practice loop. We ll discuss Strings in detail after Spring break

Chapter 8 C Characters and Strings

Strings and Library Functions

Structured programming

Copyright 2003 Pearson Education, Inc. Slide 1

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

In this chapter, you will learn about: An Array Type for Strings. The Standard string Class. Vectors. Introduction Computer Science 1 CS 23021

today cs3157-fall2002-sklar-lect05 1

String Processing CS 1111 Introduction to Programming Fall 2018

Introduction to string

Scientific Programming in C V. Strings

C Style Strings. Lecture 11 COP 3014 Spring March 19, 2018

BITG 1113: Array (Part 2) LECTURE 9

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

C mini reference. 5 Binary numbers 12

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Topic 3. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

String Objects: The string class library

Computer Programming

Object Oriented Programming COP3330 / CGS5409

EECS402 Lecture 08. Intro To The Standard string Class. Some string Functionality

Decisions. Arizona State University 1

Strings and Stream I/O

Lecture 12. We have already used strings. Strings. Hello Class is a string.

Outline. Computer Programming. Structure of a function. Functions. Function prototype. Structure of a function. Functions

Chapter 8: Character & String. In this chapter, you ll learn about;

Characters and Strings

CS 31 Review Sheet. Tau Beta Pi - Boelter Basics 2. 2 Working with Decimals 2. 4 Operators 3. 6 Constants 3.

Computer Science XII Important Concepts for CBSE Examination Questions

Strings in C++ Dr. Ferdin Joe John Joseph Kamnoetvidya Science Academy

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

CS 115 Exam 3, Spring 2010

C PROGRAMMING. Characters and Strings File Processing Exercise

LECTURE 02 INTRODUCTION TO C++

SWEN-250 Personal SE. Introduction to C

C-String Library Functions

CS3157: Advanced Programming. Outline

C Libraries. Bart Childs Complementary to the text(s)

Chapter 8: More About Strings. COSC 1436, Summer 2018 Dr. Zhang 7/10/2018

CCE1111 Programming for Engineers [C Programming Language]

Topic 2. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Unit 2: The string class

Appendix A. ASCII Character Set

Chapter 5 - Pointers and Strings

CSI33 Data Structures

Chapter 5 - Pointers and Strings

Object oriented programming C++

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Module 4 Arrays, Strings, and Pointers

Chapter 10. Arrays and Strings

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Software Design & Programming I

CISC 1110 (CIS 1.5) Introduc2on to Programming Using C++

The C++ Language. Arizona State University 1

Objectives. In this chapter, you will:

Name. CPTR246 Spring '17 (100 total points) Exam 2

Integer Data Types. Data Type. Data Types. int, short int, long int

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Working with Strings. Lecture 2. Hartmut Kaiser. hkaiser/spring_2015/csc1254.html

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

Chapter 8 Character Arrays and Strings

Pointers, Dynamic Data, and Reference Types

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10


CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers

CS106X Handout 04 Autumn 2009 September 25 th, 2009 C++ Strings

CS107 Spring 2019, Lecture 4 C Strings

Assignment 5: MyString COP3330 Fall 2017

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Working with Sequences: Section 8.1 and 8.2. Bonita Sharif

CS107, Lecture 4 C Strings

CS106X Handout 04 Winter 2018 January 8 th, 2018 C++ Strings

Dynamic arrays / C Strings

Transcription:

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

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 2

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

10.2 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 4

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 5

10.3 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!" H i t h e r e! \0 Chapter 10 slide 6

Review of the Internal Storage of C-Strings Array of chars 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 7

10.4 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 8

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 9

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 10

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 11

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

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 1234 10 ) in intchar Chapter 10 slide 13

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 14

10.8 The C++ string Class Special datatype supports working with strings #include <string> 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 15

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 16

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 17

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

string Operators OPERATOR >> << = += + [] >, >=, <, <=, ==,!= MEANING 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 19

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 20

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 21

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" Chapter 10 slide 22

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