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

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

CSCE 110 PROGRAMMING FUNDAMENTALS

Pointers, Arrays and C-Strings

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

Engineering Problem Solving with C++, Etter

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

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

Chapter 8 - Notes User-Defined Simple Data Types, Namespaces, and the string Type

Dr. Md. Humayun Kabir CSE Department, BUET

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

CS242 COMPUTER PROGRAMMING

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

6.096 Introduction to C++ January (IAP) 2009

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

Introduction to C++ Systems Programming

LECTURE 02 INTRODUCTION TO C++

Exercise 1.1 Hello world

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Variables. Data Types.

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Unit 14. Passing Arrays & C++ Strings

Computer Programming

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

C++ Programming: From Problem Analysis to Program Design, Third Edition

Lab # 02. Basic Elements of C++ _ Part1

Chapter 2: Overview of C++

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Classes, objects. Static, const, mutable members of a class

std::string Quick Reference Card Last Revised: August 18, 2013 Copyright 2013 by Peter Chapin

Streams. Ali Malik

Communication With the Outside World

a. a * c - 10 = b. a % b + (a * d) + 7 =

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Cours de C++ Introduction

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

Introduction to C++ IT 1033: Fundamentals of Programming

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

2 is type double 3 auto i = 1 + 2; // evaluates to an 4 integer, so it is int. 1 auto d = 5.0; // 5.0 is a double literal, so it

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

A brief introduction to C++

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Chapter 15 - C++ As A "Better C"

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Chapter 1 Introduction to Computers and C++ Programming

Introduction to C++ with content from

Introducing C++ to Java Programmers

Lab Instructor : Jean Lai

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

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Input And Output of C++

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

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Objectives. In this chapter, you will:

CMSC 202 Midterm Exam 1 Fall 2015

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Definition Matching (10 Points)

Scientific Computing

Computer Programming : C++

Getting started 7. Performing operations 25

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

VARIABLES & ASSIGNMENTS

C++ Input/Output Chapter 4 Topics

Lecture 23: Pointer Arithmetic

Object Oriented Design

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Exception with arguments

Chapter 3 - Notes Input/Output

2 nd Week Lecture Notes

Getting started with C++ (Part 2)

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

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++

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

pointers + memory double x; string a; int x; main overhead int y; main overhead

CSCE 110 PROGRAMMING FUNDAMENTALS

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

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Dodatak D Standardna string klasa

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

C++ Programming Fundamentals

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

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

COMP322 - Introduction to C++

CSCE 110 PROGRAMMING FUNDAMENTALS

Module 7 b. -Namespaces -Exceptions handling

Honors Computer Science C++ Mr. Clausen Program 3A, 3B, 3C

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

1. Which of the following best describes the situation after Line 1 has been executed?

Programming. C++ Basics

Transcription:

C++ Strings, Enums Data Processing Course, I. Hrivnacova, IPN Orsay Using strings Manipulating strings as fundamental types (using operators) Manipulating strings as objects (using object-oriented syntax) Useful string operations Enums I. Hrivnacova @ Data Processing Course 2019 1

C++ Strings In C++, there are no built-in types for the use of strings. The string type is provided by the standard library String classes enable you to use strings as normal types that cause no problems for the user Different from ordinary strings of type char* or const char* Copy, assignment and compare functions, as for fundamental types, are already available - no worry about memory corruption Defined in the header <string> Type: string = chain of characters of char type I. Hrivnacova @ Data Processing Course 2019 2

#include <iostream> #include <string> using namespace std; Using Strings - I // Create two strings string firstname = "bjarne"; string lastname = "stroustrup"; string name; // Manipulate strings firstname[0] = 'B'; lastname[0] = 'S'; // Chain strings name = firstname + lastname; // Compare strings if ( name!= "" ) { cout << name << " is founder of C++" << endl; firstname, lastname Variables of type string (in view of traditional programing) Objects or instances of the class string in view of object-oriented programming) In object-oriented programming types are called classes The strings can be manipulated with operators as fundamental types I. Hrivnacova @ Data Processing Course 2019 3

#include <iostream> #include <string> using namespace std; Using Strings - I // Create two strings string firstname = "bjarne"; string lastname = "stroustrup"; string name; // Manipulate strings firstname[0] = 'B'; lastname[0] = 'S'; // Chain strings name = firstname + " " + lastname; // Compare strings if ( name!= "" ) { cout << name << " is founder of C++" << endl; Program output: >./usestrings Bjarne Stroustrup is founder of C++ I. Hrivnacova @ Data Processing Course 2019 4

#include <iostream> #include <string> using namespace std; // Create a string string name = "Agatha"; // Append it with another string name.append("chriistie"); // Find position of 'Chriistie' auto idx = name.find("chriistie"); // Insert a space name.insert(idx, " "); // Find position of the first i auto idxi = name.find("i"); // Erase this character name.erase(idxi, 1); Using Strings - II Object-oriented syntax for operations that are defined for string objects objectname.function() This kind of function is called a member function in C++ The effect of these functions depends on the implementation of these operations in the string class auto automatic type deduction (C++11) // Print the result cout << name << endl; I. Hrivnacova @ Data Processing Course 2019 5

#include <iostream> #include <string> using namespace std; // Create a string string name = "Agatha"; // Append it with another string name.append("chriistie"); Using Strings - II // Find position of 'Chriistie' auto idx = name.find("chriistie"); // Insert a space name.insert(idx, " "); // Find position of the first i auto idxi = name.find("i"); // Erase this character name.erase(idxi, 1); Program output: >./usestrings2 Agatha Christie // Print the result cout << name << endl; I. Hrivnacova @ Data Processing Course 2019 6

#include <iostream> #include <string> using namespace std; // Create a string string name = "Agatha"; // Append it with another string name.append("chriistie"); // Find position of 'Chriistie' auto idx = name.find("chriistie"); // Insert a space name.insert(idx, " "); // Find position of the first i auto idxi = name.find("i"); // Erase this character name.erase(idxi, 1); Using Strings - II Object-oriented syntax for operations that are defined for string objects objectname.function() This kind of function is called a member function in C++ The effect of these functions depends on the implementation of these operations in the string class auto automatic type deduction (C++11) // Print the result cout << name << endl; I. Hrivnacova @ Data Processing Course 2019 7

Useful String Operations =, assign() Assign new value swap() +=, append(), push_back() insert() erase() clear() resize() replace() Swap values between two strings Append characters Insert characters Erase characters Erase all characters (emoty string) Change number of characters (erase or add characters at the end) Replace characters + Create sum string I. Hrivnacova @ Data Processing Course 2019 9

==,!=, <, <=, >, >=, compare() length(), size() empty() Useful String Operations Compare two strings Return number of characters Test whether a string is empty [], at Access a single character >>, getline() Feed in string << Output string find functions substr() c_str() copy() Search for a part string or character Issue a part string Use string as C-string Copy a character into a character buffer An overview of string operations: see also C++ book, chapter 3.4.3, page 67 I. Hrivnacova @ Data Processing Course 2019 10

Enumerations An enumerated type (also called an enumeration) is a data type where every possible value is defined as a symbolic constant, called an enumerator Enumerations are declared via the enum keyword. // Enumeration named Color enum Color { kred, kgreen, kblue ; // Variables of enumerated type Color Color grass = kgreen; Color sky = kblue; I. Hrivnacova @ Data Processing Course 2019 11

Enumerations Each enumerator is automatically assigned an integer value based on its position in the enumeration list. By default, the first enumerator is assigned the integer value 0 Each subsequent enumerator has a value one greater than the previous enumerator It is possible to explicitly define the value of enumerator // Enumeration named Color enum Color { kred, // assigned 0 kgreen, // assigned 1 kblue = 6, kwhite // assigned 7 ; // Variables of enumerated type Color Color grass = kgreen; Color sky = kblue; Color show = kwhite; cout << grass << endl; // 1 cout << snow << endl; // 7 I. Hrivnacova @ Data Processing Course 2019 12