Engineering Problem Solving with C++, Etter

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

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

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

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

Copyright 2003 Pearson Education, Inc. Slide 1

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

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

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

Chapter 10 Characters, Strings, and the string class

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

String Objects: The string class library

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

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

LECTURE 15. String I/O and cstring library

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 7: User-Defined Simple Data Types, Namespaces, and the string Type

BITG 1113: Array (Part 2) LECTURE 9

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

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

Lecture 3 The character, string data Types Files

System Design and Programming II

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

Built-in Functions for NTCAs.

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

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

CSCE 110 PROGRAMMING FUNDAMENTALS

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


Object Oriented Programming COP3330 / CGS5409

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

Computer Programming

C++ basics Getting started with, and Data Types.

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

Chapter 9. Strings. 國立雲林科技大學資訊工程研究所張傳育 (Chuan Yu Chang ) 博士

ONE DIMENSIONAL ARRAYS

Chapter 10. Arrays and Strings

Week 5. Muhao Chen.

Exercise 1.1 Hello world

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

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

C-String Library Functions

6. User Defined Functions I

C++ For Science and Engineering Lecture 12

CS 115 Final Review Quiz

CS 115 Exam 3, Spring 2010

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

Unit 14. Passing Arrays & C++ Strings

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 (!

The C++ Language. Arizona State University 1

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

COP 3014: Fall Final Study Guide. December 5, You will have an opportunity to earn 15 extra credit points.

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

Arrays intro (& strings) Ch 7

Definition Matching (10 Points)

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

13 4 Understanding Character Strings

Objectives. In this chapter, you will:

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Your first C++ program

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

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

CS 103 Lab 6 - Party Like A Char Star

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 6: One Dimensional Array

Computers Programming Course 10. Iulian Năstac

Dodatak D Standardna string klasa

Arrays (& strings) Ch 7

Dynamic arrays / C Strings

Week 3: File I/O and Formatting 3.7 Formatting Output

CS31 Discussion. Jie(Jay) Wang Week6 Nov.4

Homework Assignment #2 (revised)

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

2 nd Week Lecture Notes

Computer Programming

CMPS 221 Sample Final

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

12/2/2009. The plan. References. References vs. pointers. Reference parameters. const and references. HW7 is out; new PM due date Finish last lecture

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

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

COMSW Introduction to Computer Programming in C

Introduction to string

EE 355 Lab 4 - Party Like A Char Star

Name Section: M/W or T/TH. True or False (14 Points)

Chapter 2: Introduction to C++

Strings and Stream I/O

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.

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

C++ Strings, Enums. Data Processing Course, I. Hrivnacova, IPN Orsay

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Dr. Md. Humayun Kabir CSE Department, BUET

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

Objectives. Chapter 8 Arrays and Strings. Objectives (cont d.) Introduction 12/14/2014. In this chapter, you will:

Transcription:

Engineering Problem Solving with C++, Etter Chapter 6 Strings 11-30-12 1

One Dimensional Arrays Character Strings The string Class. 2

C style strings functions defined in cstring CHARACTER STRINGS 3

C Style Character Strings A C style string is defined as a sequence of characters, terminated by the null character. When declaring a character array to store a C style string, memory must be allocated for the null character ('\0'). Literal string constants are enclosed within double quote marks: "a string". 4

C Style Character Strings Example: cout << "Hello"; 'H' 'e' 'l' 'l' 'o' '\0' 5

C Style String Input Recall that the input operator (>>) skips whitespace. To input strings with embedded whitespace, the getline() function can be used as illustrated: char phrase[size]; cin.getline(phrase, SIZE); The getline() function reads up to SIZE-1 characters from the input stream and will insert the null character. getline() is a member function of what class? stringin.cpp 6

C STYLE STRING FUNCTIONS The Standard C++ library contains a set of predefined functions that operate on C style strings. These functions are defined in the header file: cstring Commonly used string functions: strlen(s1) -- returns string length of s1 strcpy(s1,s2) -- copies s2 into s1 strcat(s1, s2) -- concatenates s1 and s2 strcmp(s1, s2) --compares returns -1, 0 or +1 7

Comparing strcmp(a,b) -1 when a <b 0 when a==b 1 when a>b Same as dictionary sort. Early words are "less than" later words. "apple" < "toast"

Example: C Style Strings #include <iostream> #include <cstring> //strcmp(), strcpy(), strcat() uses namespace std; int main(){ char str1[30] = "John", str2[30] = "Joseph"; char phrase[20] = "'s shirt was green", sentence[30]; if (strcmp(str1,str2) < 0) strcpy (sentence, str1); // puts "John" into sentence else strcpy (sentence,str2); strcat(sentence, phrase); cout << "Sentence is: " << sentence << endl; return 0; } //in ch6 cstrings.cpp // puts "Joseph into sentence 9

C-string Length in Copying When using the cpystr() function the length has to be controlled. copycstring.cpp

functions defined in string THE STRING CLASS 11

The string class The string class implements the concept of a character string. A string object can increase and decrease its size dynamically. Numerous operators and functions are defined in the string class. 12

Questions What is the difference between a cstring and a string? Name some advantages of using a string over a cstring? What function can be used to input strings?

Common Functions Defined in string size( ) empty( ) substr (int start, int len) c_str() 14

Overloaded Operators Defined in string relational operators < > == <= >=!= concatenation + += assignment = 15

Example: string class #include <iostream> #include <string> using namespace std; int main(){ string str1 = "John", str2 = "Johnson"; string phrase = "'s shirt was green", sentence; if (str1 < str2) sentence = str1; // assign "John" to sentence else sentence = str2; // assign "Johnson to sentence sentence += phrase; // append phrase to sentence cout << "Sentence is: " << sentence << endl; return 0; } string.cpp 16

Another Example winner.cpp getline(cin, str); //String version of getline

Count the e's Write a program to count the number of e's in an input string. 1)Input the string. 2)len = length of the string 3)For (i = 1 to len) a)if the ith character = 'e' count++ 4.) Output count

Count the e's countes.cpp