Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Arrays and String Using array Initialized arrays of different.

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

M ANIPULATING S TRINGS Resources [1] Object Oriented Programming with C++ (3 rd Edition) E Balagurusamy [2] Teach Yourself C++ (3 rd Edition) H Schildt.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
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.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. About the book Ivor Horton, Beginning ANSI C++: The Complete.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
1 Lecture 19:String Data Type Introduction to Computer Science Spring 2006.
 2006 Pearson Education, Inc. All rights reserved Class string and String Stream Processing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Multiple-Subscripted Array
Chapter 9: Arrays and Strings
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
C++ standard strings. Standard library of C++ language STL STL (The main part of standard library of C++ language) Stream classes Stream classes String.
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.
Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
C++ String Class. Outline  String Initialization  Basic Operations  Comparisons  Substrings  Swapping Strings  String Size  Finding Strings and.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Loops: Repeating One or More Statements for loop while loop.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Arrays.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
CSC 270 – Survey of Programming Languages
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
String Class Mohamed Shehata 1020: Introduction to Programming.
SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture.
1 Lecture 12 Pointers and Strings Section 5.4, ,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 16 C++ Strings By C. Shing ITEC Dept Radford University.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
STL string class Programming Team 2/15/2006. string constructors string() = empty string string(int l, char ch) = string of length l, of repeated ch string(char*
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Program Files and Preprocessing Directives Program files and.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. More on Functions Overloaded Functions Use pointers and references.
Character Sequences. strings are in fact sequences of characters, we can represent them also as plain arrays of char elements. For example, the following.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Pointers Declare a pointer Address of a variable Pointers and.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Strings.
Fundamentals of Characters and Strings
Class string and String Stream Processing: A Deeper Look
Arrays Arrays exist in almost every computer language.
Engineering 1020: Introduction to Programming Fall 2018
Pointers and Pointer-Based Strings
Arrays November 8, 2017.
Programming with ANSI C ++
String class and its objects
String What it is Why it’s useful
Representation and Manipulation
C++ Programming Lecture 20 Strings
character manipulation
Presentation transcript:

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Arrays and String Using array Initialized arrays of different types Null-terminated string character array  string Multidimensional arrays Generate a string by character array String objects

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Arrays An array of temperatures –double temperatures[366]; Index Offset – distance between the first element to current element Program 6.1

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Initializing An Array const int max_heights = 10;//array size int height[max_heights]; int samples[5] = {2, 3, 4, 6, 11}; Program 6.2

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Initialized All Elements Initialize all elements to zero –double junk[20] = {0}; –double junk[20]={}; Declare array size by initial values –int values[ ] = {2, 3, 4}; –int values[3] = {2, 3, 4}; Using sizeof() to find the size of an array, Program 6.3

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. C-style String and char Array C-style string ends up with a null character ‘\0’ –10 elements array end up with two ‘\0’ char name[10]= “Mae West”; –9 elements array end up with one ‘\0’ char name[ ] = “Mae West”; –6 elements array Char vowels[ ] = “aeiou”; An array of characters with –char vowels[5] = { ‘a’, ‘e’, ‘i’, ‘o’, ‘u’}; //five elements –char vowels[ ] = ”aeiou”; // six elements Program 6.4

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. An array of type char const int maxlength = 100; char text[maxlength] = {0}; // input ended at ‘ ‘ or ‘\n’, regarded as a delimiter cin >> text; // input ended at ‘\n’ cin.getline(text, maxlength); // input many lines ended at ‘!’ cin.getline(text, maxlength, ‘!’);

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Multidimensional Array

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Rows and Columns

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Multidimensional Array double carrots[3][4]; for (int i=0; i<3; i++) { for (int j=0; j<4; j++) cout << setw(12) << carrots[i][j]; cout << endl; }

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Multidimensional Array double carrots[3][4]; for (int i=0; i<sizeof carrots/sizeof carrots[0]; i++) { for (int j=0; j<4; j++) cout << setw(12) << carrots[i][j]; cout << endl; }

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Multidimensional Array const int nrows = 3; const int ncols = 4; double carrots[nrows][ncols]; for (int i=0; i<nrows; i++) { for (int j=0; j<ncols; j++) cout << setw(12) << carrots[i][j]; cout << endl; }

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Initializing Multidimensional Arrays double carrots[3][4] = { {2.5, 3.4, 4.6, 6.7}, {3.6, 5.3, 4.3, 2.9}, {2.3, 4.4, 1.0, 2.0} }; Zeros for the non-initialized elements double carrots[3][4] = {{2.5, 3.4}, {3.6}, {2.3, 4.4, 1.0}}; double carrots[3][4] = {0}; double carrots[3][4] = {1.1, 1.2, 1.3, 1.4, 1.5, 1.6};

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Preset Dimensions double carrots[ ][4] = {{2.5, 3.4}, {3.6}, {2.4, 6.1, 3.2}, {2.3, 4.4, 1.0}}; Same as double carrots[4][4] = {{2.5, 3.4}, {3.6}, {2.4, 6.1, 3.2}, {2.3, 4.4, 1.0}};

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. String Array char start[6][80] = { “Robert Redford”, “Hopalong Cssidy”, “Lassie”, “Slim Pickens”, “Boris Karloff”, “Oliver Hardy” }; Program 6.5

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. string Class Declaring an empty string object –string myString; Initialize a string object –string proverb = “Many a mickle makes a muckle”; –string proverb(“Many a mickle makes a muckle);

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. string Class int letters = proverb.length(); string sleeping(6, ‘z’) ;// string sleeping = “zzzzzz”; string light_sleep(1, ‘z’); // string light_sleep = “z”; string light_sleep=‘z’; // wrong string sentence = proverb; string sentence(proverb); string phrase(proverb, 0, 13);// see p.201

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. string Class string adjective = “hornswoggling”; string word = “rubbish”; word = adjective; adjective = “twotiming”;

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. String Concatenation string description = adjective + “ ” + “whippersnapper”; Program 6.6

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Read a Line Read a line from keyboard and store in text string text; getline(cin, text); Read a line from keyboard and store in text until ‘#’ getline(cin, text, ‘#’); Program 6.7

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Some Overloaded Functions in string.h substr() compare() find(), rfind() find_first_of(), find_not_first_of() find_last_of(), find_not_last_of() insert(), replace(), erase()

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Access Substring string phrase = “The higher the fewer”; string word = phrase.substr(4, 6);// “higher” word = phrase.substr(4, 100); // till the end word = phrase.substr(4);// till the end

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. String Comparison String comparing operations are associated to ASCII sequence Program 6.8

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. The compare() Function object_name.compare(other_object) Return 1 (greater), 0 (equal), -1 (less)

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. The find() Function string sentence = “Manners maketh man” string word = “man”; cout << sentence.find(word) << endl; // 15 cout << sentence.find(“Man”) << endl; // 0 cout << sentence.find(‘k’) << endl;// 10 cout << sentence.find(‘x’) << endl; // string::npos  maximum value of (unsigned int)  2 32 –1 Programs 6.9

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. The find_first_of() Functions Return index –find_first_not_of() –find_last_of() –find_not_last_of() –rfind() – reverse find Program 6.10

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. The insert(), replace() and erase() Functions Insert a sub-string into the specific position in a string object Replace all the sub-strings in a string object Erase a sub-string in a string, or removing characters from a string Program 6.11

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Arrays of Type string string words[] = {“this”, “that”, “the other”}; string words[10] = {“this”, “that”, “the other”}; words[2][6] = ‘t’; cout << words[2]; // “the otter”