Download presentation
Presentation is loading. Please wait.
Published byValentine Day Modified over 9 years ago
1
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 11 Scott Marino
2
MSMIS Kean University Topics Strings Allocating a string variable String functions String input Numbers Characters Concatenation Sub-strings ADT Stacks Queues Lab 8 / Homework
3
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
4
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\0 012345678
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
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 [!,. ? …]
18
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
19
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
20
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
21
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
22
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”
23
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
24
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
25
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
26
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)
27
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
28
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
29
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”
30
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 */
31
Scott Marino MSMIS Kean University Pointers Pointer operators * - Dereference (given a pointer, get the thing referenced) & - Address of (given a thing, point to it)
32
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
33
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
34
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.