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

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

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

Chapter 10 Characters, Strings, and the string class

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

C: How to Program. Week /May/28

Engineering Problem Solving with C++, Etter

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.

LECTURE 15. String I/O and cstring library

Chapter 8 C Characters and Strings

Structured programming

Copyright 2003 Pearson Education, Inc. Slide 1

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

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

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

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

today cs3157-fall2002-sklar-lect05 1

Scientific Programming in C V. Strings

Strings and Library Functions

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

BITG 1113: Array (Part 2) LECTURE 9

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

String Processing CS 1111 Introduction to Programming Fall 2018

C mini reference. 5 Binary numbers 12

Characters and Strings

Introduction to string

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.

Computer Programming

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

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

Strings and Stream I/O

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

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

Object Oriented Programming COP3330 / CGS5409

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

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

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

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

CS 115 Exam 3, Spring 2010

String Objects: The string class library

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

C PROGRAMMING. Characters and Strings File Processing Exercise

Computer Science XII Important Concepts for CBSE Examination Questions

CS3157: Advanced Programming. Outline

Decisions. Arizona State University 1

Unit 2: The string class

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

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

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

Appendix A. ASCII Character Set

LECTURE 02 INTRODUCTION TO C++

C-String Library Functions

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

Module 4 Arrays, Strings, and Pointers

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

CS107, Lecture 4 C Strings

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

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

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

Chapter 5 - Pointers and Strings

Chapter 5 - Pointers and Strings

SWEN-250 Personal SE. Introduction to C

CCE1111 Programming for Engineers [C Programming Language]

Chapter 10. Arrays and Strings

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

CS107 Spring 2019, Lecture 4 C Strings

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

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


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

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

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

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

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Week 5. Muhao Chen.

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

Ch 6-8. C-Strings. April 30, Prof. Young-Tak Kim

Strings are actually 'objects' Strings

Assignment 5: MyString COP3330 Fall 2017

CSI33 Data Structures

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

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

Exercise 1.1 Hello world

Lecture 10. Command line arguments Character handling library void* String manipulation (copying, searching, etc.)

CSc Introduc/on to Compu/ng. Lecture 17 Edgardo Molina Fall 2011 City College of New York

! A C-string is a sequence of characters stored in consecutive memory locations and terminated by a null character ( \0').

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

what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8

Applied C and C++ Programming

The C++ Language. Arizona State University 1

Transcription:

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

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 10-3 From Program 10-1 See Pr10-2.cpp 4

10.2 Character Case Conversion 5 Character Case Conversion Require cctype header file Functions: toupper: if char argument is lowercase letter, return uppercase equivalent; otherwise, return input unchanged char ch1 = 'H'; char ch2 = 'e'; char ch3 = '!'; cout << toupper(ch1); // displays 'H' cout << toupper(ch2); // displays 'E' cout << toupper(ch3); // displays '!' 6

Character Case Conversion Functions: tolower: if char argument is uppercase letter, return lowercase equivalent; otherwise, return input unchanged char ch1 = 'H'; char ch2 = 'e'; char ch3 = '!'; cout << tolower(ch1); // displays 'h' cout << tolower(ch2); // displays 'e' cout << tolower(ch3); // displays '!' 7 10.3 C-Strings 8

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 9 C-Strings Array of chars can be used to define storage for string: const int SIZE = 20; char city[size]; Leave room for NULL at end Can enter a value using cin or >> Input is whitespace-terminated No check to see if enough space For input containing whitespace, and to control amount of input, use cin.getline() 10

11 10.4 Library Functions for Working with C-Strings 12

Library Functions for Working with C-Strings Require the cstring header file Functions take one or more C-strings as arguments. Can use: C-string name pointer to C-string literal string 13 Library Functions for Working with C-Strings Functions: strlen(str): returns length of C-string str char city[size] = "Missoula"; cout << strlen(city); // prints 8 strcat(str1, str2): appends str2 to the end of str1 char location[size] = "Missoula, "; char state[3] = "MT"; strcat(location, state); // location now has "Missoula, MT" 14

strncat(str1, str2, maxchar) if(strcmp(partnum, S147-29A ) == 0) < //abc before > // abc after See Pr10-8.cpp Pr10-7.cpp Pr10-6.cpp (2D char array) stricmp( ) // ignores case 15 Library Functions for Working with C-Strings Functions: strcpy(str1, str2): copies str2 to str1 const int SIZE = 20; char fname[size] = "Maureen", name[size]; 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. 16

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 river[] = "Wabash"; char word[] = "aba"; cout << strstr(state, word); // displays "abash" 17 10.5 C-String/Numeric Conversion Functions 18

String/Numeric Conversion Functions require cstdlib header file FUNCTION PARAMETER ACTION 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 itoa int,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 10-19 String/Numeric Conversion Functions int inum; long lnum; double dnum; char intchar[10]; inum = atoi("1234"); // puts 1234 in inum lnum = atol("5678"); // puts 5678 in lnum dnum = atof("35.7"); // puts 35.7 in dnum itoa(inum, intchar, 8); // puts the string // "2322" (base 8 for 1234 10 ) in intchar 10-20

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 21 Convert a number to a string object int number = 99; string output = to_string(number); 22

10.6 Writing Your Own C-String Handling Functions 10.6 Writing Your Own C-String Handling Functions 24

Writing Your Own C-String Handling Functions Designing C-String Handling Functions can pass arrays or pointers to char arrays Can perform bounds checking to ensure enough space for results Can anticipate unexpected user input 25 From Program 10-9 26

From Program 10-10 27 10.7 More About the C++ string Class 28

The C++ string Class Special data type 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; 29 30

Input into a string Object Use cin >> to read an item into a string: string firstname; cout << "Enter your first name: "; cin >> firstname; 31 32

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); 33 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 34

35 Other Definitions of C++ strings Definition string name; string myname("chris"); string yourname(myname); Meaning defines an empty string object defines a string and initializes it 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 10-36

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 10-37 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" 38

39 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 See Table 10-8 for a list of functions 40

string Member Functions string word1, word2, phrase; cin >> word1; // word1 is "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" 41 42