CS 103 Lab - Party Like A Char Star

Similar documents
CS 103 Lab 6 - Party Like A Char Star

EE 355 Lab 4 - Party Like A Char Star

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

EE355 Lab 5 - The Files Are *In* the Computer

Pointers in C/C++ 1 Memory Addresses 2

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

In this chapter, you will learn about: An Array Type for Strings. The Standard string Class. Vectors. Introduction Computer Science 1 CS 23021

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

Arrays array array length fixed array fixed length array fixed size array Array elements and subscripting

LAB #8. Last Survey, I promise!!! Please fill out this really quick survey about paired programming and information about your declared major and CS.

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

CS 103 Lab The Files are *In* the Computer

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

For example, let s say we define an array of char of size six:

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

C++ for Java Programmers

CS 61c: Great Ideas in Computer Architecture

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Name. CPTR246 Spring '17 (100 total points) Exam 2

Copyright 2003 Pearson Education, Inc. Slide 1

Exercise 1.1 Hello world

Homework #3 CS2255 Fall 2012

Assignment #1 (50 points; due 11:59 P.M.)

Object Oriented Programming COP3330 / CGS5409

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CSE 333 Midterm Exam 7/29/13

Pointers. Variable Declaration. Chapter 10

Unit 14. Passing Arrays & C++ Strings

CS31 Discussion. Jie(Jay) Wang Week6 Nov.4

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

CS 115 Exam 3, Spring 2010

CA31-1K DIS. Pointers. TA: You Lu

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

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

CS106L Handout #05 Fall 2007 October 3, C Strings

Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

C-String Library Functions

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

C++ For Science and Engineering Lecture 12

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

Lab#5 Due Wednesday, February 25, at the start of class. Purpose: To develop familiarity with C++ pointer variables

Homework Assignment #2 (revised)

The C++ Language. Arizona State University 1

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes...

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

CS31 Discussion 1E Spring 17 : week 08

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

BITG 1113: Array (Part 2) LECTURE 9

Week 5. Muhao Chen.

C Style Strings. Lecture 11 COP 3014 Spring March 19, 2018

Programming Studio #9 ECE 190

Linked lists Tutorial 5b

Lecture 12. We have already used strings. Strings. Hello Class is a string.

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

C Characters and Strings

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

CSE 333 Midterm Exam 5/10/13

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

CS61 Lecture II: Data Representation! with Ruth Fong, Stephen Turban and Evan Gastman! Abstract Machines vs. Real Machines!

6. Pointers, Structs, and Arrays. 1. Juli 2011

Pointers and References

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

EE 109 Lab 8a Conversion Experience

EE 355 Lab 3 - Algorithms & Control Structures


Engineering Problem Solving with C++, Etter

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Operator overloading

CS61C : Machine Structures

To become familiar with array manipulation, searching, and sorting.

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

C++ For Science and Engineering Lecture 15

CS 103 Unit 11. Linked Lists. Mark Redekopp

Lab 9: Pointers and arrays

CS 103 Unit 11. Linked Lists. Mark Redekopp

Week 3. This is CS50. Harvard University. Fall Cheng Gong

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Exercise: Inventing Language

C++ Lab 02 - Command Line Arguments and Strings

CS 150 Lab 10 Functions and Random Numbers

Lab: Supplying Inputs to Programs

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

13 4 Understanding Character Strings

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

CS 103 Lab V for Vector

Today in CS161. Lecture #12 Arrays. Learning about arrays. Examples. Graphical. Being able to store more than one item using a variable

Patterns: Working with Arrays

CSCE Practice Midterm. Data Types

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

CS2141 Software Development using C/C++ C++ Basics

CS61C : Machine Structures

Transcription:

1 Introduction In this lab you will implement a "hangman" game where the user is shown blanks representing letter of a word and then tries to guess and fill in the letters with a limited number of guesses. 2 What you will learn After completing this lab you should be able: Manipulate C-string variables (variables of type char*) Pass C-strings to functions Utilie the C-string library (#include <cstring>) to perform various string operations Use cin to read a C-string 3 Background Information and Notes Remember that C is the older language upon which C++ is based. (Most C programs work in C++ too.) We want to manipulate chunks of text, or strings, in our programs. The kinds of strings we ll be dealing with for the next few weeks are the ones from the C language, so-called C strings. (Towards the end of the course we ll discuss C++ string objects.) As we ve seen, a C string can be thought of as an array of characters (a char[] variable, see section 7.3.2 of the book, and the lecture notes). But to better understand C strings, recall: 1) C adopts the convention that the last significant character in a string should be followed by the null character '\0'. 2) When you call a function that takes an array as an input, all it actually sends is the memory address (a pointer to the start of the array). Item 1) makes a lot of sense in light of 2): even though a function only knows the starting address of a C string passed to it, it can also figure out where it ends, by following each character in sequence one at a time until reaching a null character. A char* variable, or a char pointer variable, stores the address of a character. Item 2) means that from the perspective of a function, a char* argument is the same as a char[] argument. (Due to pointer arithmetic, you can even write ptr[i] when ptr is a char* rather than a char[].) So the standard C/C++ string libraries are written to accept char* arguments. (The library generally uses char* everywhere rather than char[], one reason being that a function can return char* but not char[]). Last Revised: 10/20/2017 1

Here are a few of the most useful string functions in the standard <cstring> library: sie_t strlen ( const char * str ); char * strcpy ( char * destination, const char * source); char * strncpy ( char * destination, const char * source, sie_t num ); int strcmp ( const char * str1, const char * str2 ); strlen(): returns the length of a given string. The return type sie_t is some kind of unsigned integer (on your VM it is an unsigned 64-bit integer) but you can assign the value returned into an 'int' variable without worrying too much (unless you have strings with over 4 billion characters ). The const means it is guaranteed not to write memory to the pointer. strcpy(): This function takes in two pointers: the first to a destination character array, the second to a source character array. It copies the source string into the destination. The problem with strcpy() is that it only stops copying when it hits the NULL character in the source string. If the destination array does not have enough memory allocated, strcpy() will gladly overwrite out-of-bounds memory, which can lead to crashes, security vulnerabilities, or hacks in software that would let some rogue user take over the system. Search YouTube for Super Mario World Credits Warp for a closely related vulnerability. strncpy(): This is a safer alternative to strcpy(). It provides a third argument which is a MAXIMUM number of characters that will be copied. It should usually be passed in as the LENGTH of the destination array to ensure we don't write off the end of the destination array. What is the return value? Google 'strncpy' and check on cplusplus.com or cppreference.com to find out. strcmp(): strcmp() takes two pointers to character strings and compares them lexicographically. It returns the integer value 0 if they are equal, a negative number if str1 is "less-than" str2 and a positive number if str1 is "greater than" str2. So comparing "abc" and "abc" would return 0. Comparing "ab" and "abc" would return -1 as would comparing "ab" with "ac". Comparing "ac" with "ab" would return +1 as would comparing "abc" with "ab". 2 Last Revised: 10/20/2017

4 Procedure 4.1 [1 pt.] Finding C++ Documentation Show your TA/CP the documentation for strcmp() 4.2 [2 pts.] Copying strings Common Errors Type in the following program to a file (stringdemo.cpp). Recall that if we have the following declarations of character arrays and pointers, we cannot simply copy one string to the other via an assignment statement. Similarly pointing one char* at another string means we will just reference the original and not make a copy. 1: #include <iostream> 2: #include <cstring> 3: 4: using namespace std; 5: 6: int main() 7: { 8: char mystring[80] = "ComputerScience"; 9: char yourstring[80]; 10: char* astring = 0; 11: 12: yourstring = mystring; // does this make a copy? 13: strncpy(astring, mystring, 80); // does this make a copy? 14: 15: astring = mystring; // is this legal assignment? 16: strncpy(yourstring, mystring, 80); // make a copy? 17: cout << astring << endl; 18: cout << yourstring << endl; 19: 20: mystring[0] = '*'; // which one actually made a copy? 21: cout << astring << endl; 22: cout << yourstring << endl; 23: 24: return 0; 25: } Try to compile your code: $ compile stringdemo.cpp You should get the following error stringdemo.cpp:12:16: error: array type 'char [80]' is not assignable yourstring = mystring; ~~~~~~~~~~ ^ Recall that array names when used in C yield the starting address. So you are really saying take the starting address of mystring and make it the starting address of yourstring. But yourstring has to stay where it is. Thus you should think of array names as constant pointers that cannot be changed/assigned to. Last Revised: 10/20/2017 3

The moral of the story is you cannot assign one array to another. It will NOT make a copy as we might have thought. So now comment that line (i.e. change it to) //yourstring = mystring; Look at the next line of code and think what this will do. strncpy(astring, mystring, 80); It will attempt to copy the characters one at a time from mystring to the memory that astring points to. But what does astring point to? Compile it directly using clang++ first: $ clang++ -g stringdemo.cpp -o stringdemo $./stringdemo This should yield: Segmentation fault (core dumped) Not good, it crashed! The reason is that we haven't initialied astring. So whatever garbage bits are in the astring pointer will be used as an address and strncpy will attempt to copy the characters there causing it to crash. Now compile again using the compile command: $ compile stringdemo.cpp This time it actually warns you (compile is just clang++ with certain warning options and debug information enabled). The moral of the story is that you can only copy data to pointers that actually POINT at memory you have allocated! If a pointer doesn t point at anything, DON T USE IT. Whenever you get a segmentation fault (which will be a lot in your CS career), you can use GDB to find the line of code causing it. Open the executable in gdb: $ gdb stringdemo At the gdb prompt just type 'run' to run the program in the debugger. When it crashes in gdb, type 4 Last Revised: 10/20/2017

backtrace This will show you the function calls (and line numbers). You should see that the last function that you recognie (i.e. you wrote) is main() at stringdemo.cpp:13. This means main() called some other function at line 13 and that other function triggered the error. So this can always be used to tell you where your segmentation fault is. (If you didn t get a line number, maybe you forgot the -g when calling clang++. That option turns on the debugging information and line number tracking. Gdb will also say warning: no loadable sections found in added symbol-file. Compile again with clang++ -g or compile.) So now comment out line 13 as well: // strncpy(astring, mystring, 80); Look at line 15. We initialie astring to point at mystring. But are we making a copy? No, we re just referencing it (i.e. pointing at the original mystring array). On line 17 we print out astring which is really just printing out the data from mystring (since astring just points to mystring). But in line 20 we change mystring. So when we print out astring again on line 21 we will see the update. The moral of the story is pointing one pointer to someone else's data DOES NOT make a copy of the data. It only references that data. A change in the original is seen by the referencing pointer. On line 16 we actually make a copy of all the data from mystring to yourstring. Since we actually allocated an array of characters for yourstring, we have room to put the copied data. Now on line 20 when we change mystring, yourstring is unaffected and when we print it on line 22 we have the original. Show your TA you know how to find the source of a 'segmentation fault' by uncommenting the line of code that will cause the seg fault to occur and then running gdb in front of your TA to display the line number that caused the fault. Last Revised: 10/20/2017 5

4.3 [7 pts.] Hangman Download the skeleton code. $ wget http://bytes.usc.edu/files/cs103/lab-cstrings/hangman.cpp Note wordbank, an array of 10 strings (char *s). Your program should do the following: 1. Select a word at random from the wordbank. This is done for you. 2. On each turn display the word, with letters not yet guessed showing as *'s, and letters that have been guessed showing in their correct location 3. The user should have 10 attempts ( lives ). Each unsuccessful guess costs one attempt. Successful guesses do NOT count as a turn. 4. You must use the C-String library (#include <cstring>) functions strlen() and strcmp() in your program [strcmp() can compare when the guessed word matches the target/selected word from the wordbank meaning the user won/finished] 5. You must complete and use the function int processguess(char* word, const char* targetword, char guess) This function should take the pointer to the current value of the word guessed thus far, the pointer to the target/selected word from the wordbank, and the character that the user guessed. It should change any *'s to actual good characters for the letter the user guessed and return a count of how many times the guessed letter appears in that word. In this way, if you return 0, it means the user guessed a letter that did NOT appear and thus should lose a turn back in your main(). 6. At every turn display the current version of the guessed word and how many turns remain 7. At the end of the game give a descriptive message whether the user won or lost Demonstrate your program and show your TA/CP the two functions you wrote explaining how it works. Sample runs of our solution to this program are shown below. A winning example: * 10 remain...enter a letter to guess: o * 9 remain...enter a letter to guess: l * 6 Last Revised: 10/20/2017

8 remain...enter a letter to guess: c Current word: *c***c* 8 remain...enter a letter to guess: s Current word: sc***c* 8 remain...enter a letter to guess: g Current word: sc***c* 7 remain...enter a letter to guess: i Current word: sci**c* 7 remain...enter a letter to guess: e Current word: scie*ce 7 remain...enter a letter to guess: n The word was: science. You win! A losing example: 10 remain...enter a letter to guess: 9 remain...enter a letter to guess: 8 remain...enter a letter to guess: 7 remain...enter a letter to guess: 6 remain...enter a letter to guess: 5 remain...enter a letter to guess: 4 remain...enter a letter to guess: 3 remain...enter a letter to guess: 2 remain...enter a letter to guess: Last Revised: 10/20/2017 7

1 remain...enter a letter to guess: Too many turns...you lose! 8 Last Revised: 10/20/2017