ACS 168 Structured Programming Using the Computer Spring 2002 Joaquin Vila Prepared by Shirley White.

Slides:



Advertisements
Similar presentations
Chapter 9 Strings. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-2 Learning Objectives An Array Type for Strings C-Strings Character.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
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.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
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:
Chapter 10.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 Strings and Vectors.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes.
1 Chapter 10 Characters, Strings, and the string class.
Introduction to C programming
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
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.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real.
February 14, 2005 Characters, Strings and the String Class.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Chapter 8 Arrays and Strings
Array with base type char One character per indexed variable One extra character: '\0' Called ‘null character’ Delimiter of the string To declare a string,
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.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
9-1 Learning Objectives  An Array Type for Strings  C-Strings.
1 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 10 Strings and Multidimensional Arrays Slides by David B. Teague, Western Carolina.
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.
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 © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 8 Strings and Vectors.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 Strings and Vectors.
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 
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
CSC 270 – Survey of Programming Languages
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
Chapter Characters, Strings, and the string class 10.
Chapter 8 Strings and Vectors. Slide 8- 2 Overview 8.1 An Array Type for Strings 8.2 The Standard string Class 8.3 Vectors.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Chapter 8 Strings and Vectors 1
Chapter 8 Strings and Vectors
Chapter 9 Strings Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CPS120: Introduction to Computer Science
CS 144 Advanced C++ Programming February 7 Class Meeting
Presentation transcript:

ACS 168 Structured Programming Using the Computer Spring 2002 Joaquin Vila Prepared by Shirley White

2 Chapter 10 Strings and Multidimensional Arrays We will not use cstrings, however you should read and understand section 10.1 The next couple slides will briefly discuss cstrings.

3 cstring Values and cstring Variables A cstring variable is a partially filled array having base type char l The cstring variable signals the last used position by placing the special character, called the null character ‘\0’ in the array one position beyond the last character of the cstring. l If we declare and initialize a cstring variable s: l char s[10] ; l If s contains “Hi, Mom” then the array elements are: l s[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8] s[9] s[10] H i, M o m ! \0 ? ? l The character ‘\0’ is the sentinel marking the end of the cstring.

4 cstring Values and cstring Variables (cont.) l It is possible to initialize a cstring variable at declaration: char my_message[20] = “Hi there.”; l Notice that cstring variables need not fill the entire array. l The compiler will count the characters in the initializing string and add one for the null character: char short_string[ ] = “abc”; l This is equivalent to char short_string[4] = “abc”; or char short_string[4] = {‘a’, ‘b’, ‘c’, ‘\0’}; l You must leave space for the null character when you specify size.

5 cstring Values and cstring Variables (cont.) l A cstring is an ordinary array with base type char, and may be processed an element at a time: l This loop will change the cstring, our_string, into a cstring having the same length but with characters all ‘X’: int index = 0; while (our_string[index] != ‘\0’) { our_string[index] = ‘X’; index++; } l In processing cstrings take great care not to overwrite the null character. An array that was a cstring that has its terminating character overwritten is NO LONGER a cstring. l In the loop above, if our_string has no null terminator, the loop will run off into memory, happily writing on every byte in memory beyond the end of our_string until a byte is found with zero value.

6 PITFALL: Using = and == with cstrings (1 of 5) Values and variables of type cstring when used with = (assignment) and == (comparison for equality) do not behave like built-in types. Assigning a value to a cstring in the obvious way is illegal: char a_string[10]; a_string = “hello”; // ILLEGAL Initializing at declaration is straight forward: char a_string[10] = “DoBeDo”; The = does not mean assignment, in spite of the appearance. In Chapter 11 we will see that in C++, assignment and initialization can have very different behavior.

7 PITFALL: Using = and == with cstrings (2 of 5) Assignment can be done barehanded, an element at a time: char a_string[10] = "Hello"; char b_string[10]; int i = 0; while(a_string[i] != ‘\0’) b_string[i] = a_string[i]; There is a predefined function, strcpy, to assign cstrings: char a_string[10] = "Hello"; char b_string[10]; strcpy(b_string, a_string); The strcpy function is declared in the header.

8 PITFALL: Using = and == with cstrings (3 of 5) Comparision of cstrings cannot be done with the == operator. The attempt to compare cstrings with == compiles, it does not get the results you expect. Array names carry the address value of the first array element. The result of using == depends on where in memory the cstrings are stored. We leave it to the student to compare strings barehanded, and illustrate use of the predefined comparison function strcmp. char a_string[10] = "aeolean"; char b_string[10] = "aeonian"; if (strcmp(b_string, a_string)) cout << "The strings are NOT the same."; else cout << "The strings are the same."; The strcmp function is declared in the header.

9 PITFALL: Using = and == with cstrings (4 of 5) l strcmp compares cstrings is in lexicographic order: l For successive values of i starting at 0, cstring1[i] and cstring2[i] are compared: n If the characters are different: –If cstring1[i] < cstring2[i] strcmp returns a negative number. –If cstring1[i] > cstring2[i], strcmp returns a positive number. –The number may be -1 or +1, or the difference of the encoding (cstring1[i] - cstring2[i]), or some other value. The actual value returned depends on the implemenation. Do not write code that depends on the value returned. –T esting then stops. n If the cstrings are equal up to the end of one of them, the value returned indicates the longer string is greater than the shorter string. n If the strings are equal in length and have the same characters, the strings are equal.

10 PITFALL: Using = and == with cstrings (5 of 5) Display 10.1 contains a few of the functions from the cstring library. You must #include to gain access to these functions. strcpy(target, source) replaces target with source. Be sure there is enough space in target to hold all of source. strcat(target, source) appends source to target. The first character of source is copied into the null terminator of target, and all successive characters of source are copied into target. Be sure there is enough space in target for all of source’s characters, including source’s null terminator. strlen(source) returns the number of characters up to but not including the null terminator. strcmp(str1, str2) We discussed this in an earlier slide. Refer to Display 10.1 for detail.

11 Dangers in Using Functions from There is a very real danger associated with the functions strcpy and strcat. Both these functions copy characters until a null character is found in the source string, without regard to whether space is available in the target. If there is not space in the target, strcpy and strcat will happily overwrite any variables in memory beyond the target array. This may be some of your variables, or it could be something that your system depends on to run correctly. There could be no effect what so ever. There could be a segmentation violation or illegal operation error, with your program crashing, and no further problems. The operating system could crash and burn. Nothing apparent may happen. But the next application started could crash and burn on loading. Be careful.

12 cstring-to-number Conversions and Robust Input (1 of 3) ‘1’, “1” and 1 are different. 1 is a int constant, also called a literal. ‘1’ is a char constant. It occupies one byte and is represented by some encoding. In C++ the value is the ASCII encoding, which has the decimal value 49. (There is a new encoding called unicode characters. The C++ type that holds unicode is wchar_t. You study this in later courses.) “1” is a cstring constant. It occupies two bytes, one for the encoding of the character 1 and one for the null terminator. In a program in any language, you cannot ignore the difference between these objects. Robust numeric input may be written by inputting a cstring, extracting the digit characters and building the number from the digits.

13 cstring-to-number Conversions and Robust Input (2 of 3) Once you have a cstring containing the digits that represent an int, use the predefined function atoi atoi is named and pronounced: Ascii TO Integer) atoi takes a cstring argument and returns the int value represented by the digit characters in cstring. atoi returns 0 if the cstring contains a non-digit character. Example: atoi(“#AB”) returns 0. The atoi function is declared in the header. Display 10.3 has two utility functions: – read_and_clean that inputs a string, ignoring any non-digits entered. – new_line that discards all input remaining on the line.

14 cstring-to-number Conversions and Robust Input (3 of 3) The function atof is named and pronounced Ascii TO Floating point. atof is similar to atoi. It converts its cstring argument to the double value the cstring represents. Like atoi, the function atof returns 0.0 if the cstring argument does not represent to a double. Display 10.3 demonstrates read_and_clean, and Display 10.4 is demonstrates Robust Input Functions

Multidimensional Arrays Multidimensional Array Basics (1 of 2) It is useful to have an array with more than one index. In C++, this is implemented using an array with an array type as base type. Such an array is declared as following: char page[30][100]; There are 30*100 indexed variables for this array. The indexed variables for this array are: page[0][0], page[0][0],.... page[0][99] page[1][0], page[1][1],.... page[1][99] page[2][0], page[2][1],.... page[2][99]... page[29][0], page[29][1],... page[29][99]

16 Multidimensional Array Basics (2 of 2) We said that a two-dimensional array is an array with a base type that is an array type. In other words, two-dimensional array is an array of arrays. The array char page[30][100]; is a one dimensional array of size 30, whose base type is an array of size 100 with base type char. Each entry in the array of size 30 is an array of char of size 100. Most of the time the programmer can treat a two-dimensional array as if it were an array with two indices. There are two situations where being an arrays of arrays is evident: One is when a function with an array parameter for a two dimensional array: void display( const char p[ ][100], int size); With a two-dimensional array parameter the first dimension is ignored even if specified, and the compiler does not use it. This necessitates a size parameter. This makes sense if you think of the multidimensional array parameter as an array of arrays. The first dimension is the index, the rest describe the base type. With a higher-dimension array parameters the first dimension is (usually) not specified, but all the rest of the dimensions must be specified.

17

18

19 Practice Declare an array of characters to hold multiple choice answers for up to 30 questions for up to 40 students. Page 146

20 Function Practice Write a function that accepts three array parameters: the one you declared in the previous problem, a char array of 30 items, and an integer array of 40 items. The function should also accept the number of students, and the number of questions. Using the character array as the key for the exam, score each student’s exam and store the resulting score in the appropriate slot in the integer array. Each problem is worth 2 points.

21 Homework Write a program that helps score a miniature golf game. The program should ask for the number of players and the request the scores, hole by hole. After all data is entered the program should report which player won and then provide statistics: for each player, his/her total score, how many holes in one and how many holes over 4; for each hole, which player was best (ties should list all players who are tied). Use indices 1-18 for the holes and index 0 for each player’s total. Page 147

The C++ Standard string class Using cstrings with predefined cstring functions is not as safe as we would like. Using strcpy to copy a longer cstring to another (shorter) cstring will overwrite memory that may be important to your program. If you are fortunate, it will be only your program that is the casualty. Your operating system may crash. The Standard Library supplied class string provides far more utility than the cstrings C++ gets by way of its C heritage. Class strings behave very much like built-in data types and are far safer than cstrings.

23 Interface for the Standard Class string (1 of 4) Let s1, s2, and s3 be objects of class string, and suppose s1 and s2 have string values. Then + may be used for concatenation: s3 = s1 + s2; Additional space needed is allocated for s3 automatically. The default constructor generates an empty string There is a constructor that takes a cstring argument: string phrase, word1(“Hello “), word2(“World”); phrase = word1 + word2; cout << phrase << endl; The output will be Hello World

24 Interface for the Standard Class string (2 of 4) You can concatenate one string literal with a class string object: string phrase, word1(“Hello“), word2(“World”); phrase = word1 + “ “ + word2; cout << phrase << endl; The output will be Hello World

25 Interface for the Standard Class string (3 of 4) The class string overloads the > operators with stream left arguments and string right hand arguments with familiar behavior. Overloaded >> operator skips leading whitespace and reads nonwhite characters up to the next white space. To get an entire line of input for class string objects, we use the getline() function.

26 Interface for the Standard Class string (4 of 4) Characteristic use of the getline function follow: #include using namespace std; //... string str1; getline(cin, str1); //insert into str1 all input up to ‘\n’ //getline discards the ‘\n’ Getline(cin, str1, ‘.’); //insert into str1 allinput until a period is found //getline discards the period

27 Arrays of strings Remember, string is a type that acts exactly like any other type. You can have arrays whose base type is string: string list[20]; This is an arrray of 20 string objects. This array can be filled as follows: cout << “Enter 20 names, one per line: \n”; for (int i = 0; i < 20; i++) getline(cin, list[i]); The array can be viewed as follows for (int i = 0; i < 20; i++) cout << list[i] << endl;

28 Practrice Write a C++ code segment to read a name from a file in the form Last First and store it in string variable name as First Last. Declare all relevant variables. Page 150

29 String Class Operations Constructors –Can initialize with a cstring value or a string object Element access –str[i] –str.substr(position, length) –str.c_str() –str.at(i) str.empty()

30 Modifying strings str1 = str2 (does work for strings, not for cstrings) str1 += str2 str1 + str2 str.insert(pos, str2) str.remove(pos, len)

31 Comparisons Overloads all relational operators str.find(str1) str.find(str1, pos) str.find_first_of(str1, pos) str.find_first_not_of(str1, pos)

32 Problem 1 Write a function that accepts a string line, an integer fieldNum, and a string reference parameter field. line is a comma-delimited string, and the function is to get from the line and store in field the appropriate portion of line indicated by fieldNum. So if line is “Hawkins,Ann,arhawki,SR, ” and fieldNum is 3, field should be arhawki. Page 151

33 Problem 2 Write a program to read a file of data about employee raises and create a file of notes to the employees about their raises. The data is stored in the file “a:newSal.txt” in the form: LastName, FirstName OldSal NewSal For each line, write to “a:letters.txt” a note in the form: Dear FirstName LastName, You have received a raise of amount Your new salary is NewSal. Write 3 blank lines before each note and 3 blank lines followed by a line of dashes after each note. Page 152