Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 11 Scott Marino.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 10.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
CS Nov 2006 C-strings.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Stacks, Queues, and Deques
Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: , 8.11 Homework #3.
1 Chapter 10 Characters, Strings, and the string class.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Grade 12 Computer Studies HG
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 10 Scott Marino.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
February 14, 2005 Characters, Strings and the String Class.
Input, Output, and Processing
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
1 C++ Syntax and Semantics, and the Program Development Process.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
Characters, Strings, And The string Class Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Strings Chapter 5.
Chapter Characters, Strings, and the string class 10.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 1 Scott Marino.
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Topics Designing a Program Input, Processing, and Output
Data Structure By Amee Trivedi.
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Dr. Bernard Chen Ph.D. University of Central Arkansas
Characters, C-Strings, and More About the string Class
Standard Version of Starting Out with C++, 4th Edition
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Popping Items Off a Stack Lesson xx
Pointers, Dynamic Data, and Reference Types
10.1 Character Testing.
Engineering Problem Solving with C++, Etter
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Introduction to Data Structure
Topics Designing a Program Input, Processing, and Output
Standard Version of Starting Out with C++, 4th Edition
Topics Designing a Program Input, Processing, and Output
Introduction to C EECS May 2019.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 11 Scott Marino

MSMIS Kean University Topics Strings Allocating a string variable String functions String input Numbers Characters Concatenation Sub-strings ADT Stacks Queues Lab 8 / Homework

Scott Marino MSMIS Kean University Strings Strings are collections of characters A string is stored in the computer’s memory as a contiguous set of characters A string is actually an array of char data A special character, the null terminator “\0” is used to denote the end of the string data within the array A string value is a null terminated character array

Scott Marino MSMIS Kean University Strings The above string array of 8 characters, occupies 9 bytes of memory –Positions 0-7 contain the characters MSAS5004 –Position 8 contains the null terminator character MSAS5004\

Scott Marino MSMIS Kean University String vs. Char A character element (char) is denoted as ‘A’ in C++, by using single quotes –A character element occupies a single byte of storage Using double quotes, instructs C++ to treat the value as a string An “A” is treated as a string value –It occupies 2 bytes of storage, one for the character A and one for the null terminator

Scott Marino MSMIS Kean University Strings There is no base data type known as “string” A string is actually a class which is used to create objects of type string The methods available to string objects mimic the actions that can be performed on other data types Strings behave so much like base data types that they are often referred to as string variables

Scott Marino MSMIS Kean University Strings in memory When a string is first allocated, it contains enough bytes to handle the initial value Assigning a longer value to the string will force the program to allocate a larger memory block –The string class handles the allocation and de-allocation of the memory required for this operation Assigning a shorter value to a string probably will not re-allocate the string to a smaller space –Dependent upon the compiler

Scott Marino MSMIS Kean University Creating Strings #include –A header file for all string functions must be included in the program to make the string functions available Syntax: –string first_name; Allocates an object named first_name with no starting value, and no memory allocated first_name = “Joe” –Allocates at least 4 bytes of storage for the first_name string

Scott Marino MSMIS Kean University Creating Strings Syntax –string first_name (20, ‘\0’); Creates a string object named first_name with at least 20 bytes and initializes each position to null For performance reasons, this is has some advantages –Allocating a string with the length of the longest expected value means that the program will not have to allocate and de-allocate memory as the size of the string increases

Scott Marino MSMIS Kean University String Functions There are many pre-defined functions written for the string class cout << “The first name is “<< first_name.size() << “ characters long\n”; The size() property of the sting returns the number of characters in the string array populated up to the first null terminator

Scott Marino MSMIS Kean University String Functions cout << “The capacity of first name is “ << first_name.capacity() << endl; The capacity() property is the largest value that can be stored without having the program allocate a larger memory location –You can assign a larger value, it will just make the program allocate a larger memory block first_name.resize(30); –Instructs the program to resize the amount of storage for first_name

Scott Marino MSMIS Kean University String Input The cin operator is still used to read keyboard input cout << “What is your full name?”; getline(cin, full_name, ‘\n’); The getline function has 3 parameters –cin - the command being executed –full_name - the name of the string variable to assign the input to –\n - the delimiter - get all data up until the new line character is encountered

Scott Marino MSMIS Kean University Inside a string The individual characters inside the string array can be accessed for (x=0; x < first_name.size(); x++) { cout << first_name[x]; } The above loop will print the first_name string character by character Using first_name[x] allows the program to access each individual character within the array

Scott Marino MSMIS Kean University Convert a string to a number There may be instances where the input string contains numbers and it needs to be converted to a number Syntax: –integer-variable = atoi(&string-variable); Converts the alphanumeric characters (0-9) starting a the first position listed in the string to an integer The conversion includes all numeric characters until the first character of an unexpected data type is found Note that the “&” means the data is being passed to the conversion function by reference

Scott Marino MSMIS Kean University Convert a string to a number The string variable passed to the conversion routine can have a starting location other than the first character in the array integer-variable = atoi(&string-variable[some- position]); –some-position is an offset in the array atof - converts an alphanumeric character to a floating point number, including handling of the decimal point

Scott Marino MSMIS Kean University Convert a string to a number atol - converts alphanumeric value to a long integer value When converting, keyboard input, keep in mind that the person entering the data may not always type a value as expected

Scott Marino MSMIS Kean University Character Classifications The C++ string class has functions that are used to test individual characters within a string array –Can test for alphabetic [A-Z, a-z] –Can test for numeric [0-9] –Can test for alphanumeric [A-Z, a-z, 0-9] –Can test for blank space –Can test for upper case [A-Z] and lower case [a-z] –Can test for punctuation marks [!,. ? …]

Scott Marino MSMIS Kean University Character Classifications The syntax for these character classifications is the same as that used in validation type functions isalpha(string-variable[position]); –Will return a 0 (false) if the character is not an alphabetic value –Will return a non-zero (true) value if the character is alphabetic Functions for character classification include isalnum, isapha, isdigit, islower, isupper, isspace, ispunct

Scott Marino MSMIS Kean University Character Conversions There are two functions available for alphabetic characters to convert the case –From upper to lower –From lower to upper string[position] = toupper(string[position]); –If the value at the position is a lower case alphabetic value, convert it to an upper case value –If it is not a lower case alphabetic value, no conversion is needed tolower is the opposite of toupper

Scott Marino MSMIS Kean University String Concatenation The process of merging 2 or more strings into a single string is known as concatenation string first_name = “Joe”; string last_name = “Smith”; … full_name = first_name + “ “ + last_name; –The “+” plus sign is the concatenation operator –A blank space is included to separate the to fields for formatting reasons

Scott Marino MSMIS Kean University Sub-Strings The process of referencing a part of a string is know as using a sub-string Syntax: –some-string = mystring.substr(start-position, number- of-chars); This will assign the characters from the starting position, for the number of positions specified from the variable mystring to the variable some- string

Scott Marino MSMIS Kean University Sub-Strings string first_name = “Johnathan”; string short_name; … short_name = first_name.substr(0,4); cout << short_name << endl; This will take the first 4 positions of first_name and assign them to short_name It will print “John”

Scott Marino MSMIS Kean University Abstract Data Type Definition - a set of values (structures) and a collection of operations (functions) on those values ADT’s values are accessed only through the operations The physical implementation of the data structures is “hidden” through the use of operations to access the data

Scott Marino MSMIS Kean University Stacks A stack is a special kind of list where all the insertions and deletions take place at one end called the “top” LIFO - Last In / First Out –Objects are placed on top of the pile, and removed from the top of the pile in the reverse order that they were placed there

Scott Marino MSMIS Kean University Stacks ADT Operations on a stack –MAKENULL(stack) or INIT(stack) Empties the stack or creates a new empty stack –PUSH(element, stack) Insert the element on the top of the stack –EMPTY(stack) Test for the presence of elements on the stack –Returns “true” if there are any elements on the stack, “false” if the stack is empty

Scott Marino MSMIS Kean University Stacks TOP(stack) –Return the first element on the stack (does not remove it) POP(stack) –Remove the first element of the stack May return the element based upon the implementation of TOP(stack)

Scott Marino MSMIS Kean University Queues A queue is a special kind of list where insertions take place at the “rear” or “end” and deletions take place at the “front” or “head” FIFO - First In / First Out –Objects are placed onto the end of the line and removed from the front of the line by processes known as enqueue and dequeue

Scott Marino MSMIS Kean University Queues ADT Operations on a queue –MAKENULL(queue) or INIT(queue) Empties the queue or creates a new empty queue –FRONT(queue) Returns the first element on the queue –EMPTY(queue) Returns true if the queue is empty –FULL(queue) Returns true if the queue is full

Scott Marino MSMIS Kean University Queues ADT operations on a queue –ENQUEUE(element, queue) Inserts an element at the end of the queue –Adding “the last one in” –DEQUEUE (queue) Deletes the first element of the queue and returns it –Returning a value is dependant upon the implementation of front –Removing “the first one in”

Scott Marino MSMIS Kean University Pointers There are “things” and “pointers to things” Every thing has a memory address in the computer Pointers point to the memory addresses in the computer int thing; /* define a thing */ int *thing_ptr; /define a pointer to thing */

Scott Marino MSMIS Kean University Pointers Pointer operators * - Dereference (given a pointer, get the thing referenced) & - Address of (given a thing, point to it)

Scott Marino MSMIS Kean University Pointers Pointer Syntax –thing - Simple thing (variable) –&thing - Pointer to variable thing –thing_ptr - Pointer to an integer (not necessarily thing) –*thing_ptr - Integer

Scott Marino MSMIS Kean University Linked Lists A linked list is a chain of items where each item points to the next item in the chain Very flexible because the size changes with the number of elements A linked list structure is implemented as a structure with data and pointer to the next element

Scott Marino MSMIS Kean University Homework Given the “Mad Libs” sheet, write a program that will prompt for the required words (nouns, verbs, adjectives, adverbs, etc) After inputting the words, the program should print the results integrated into the story –Can use array of strings or individual variables Run the program, entering words of the correct type (noun, verb, etc.) and turn in the program and the resulting output