09S-Section-Solution

09S-Section-Solution - CS106X Winter 2008 Handout 09S...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106X Handout 09S Winter 2008 January 16 – 18, 2008 Section Solution 1 Problem 1: Compiler, compiler, what do you want? The line that will not compile in this example is the line that performs the assignment dir = num ; . Why not? Enumerated types are represented as integers at runtime, but at compile time, the C++ compiler provides addition type checking information to make your code safer. In this case, since num is in integer type, the compiler will not let you assign an integer to an enumeration. Could this be fixed? Of course! All we need to do is to add in a typecast so that the compiler’s convinced that you know what you are doing. The following line does the same thing, but will actually compile: dir = directionT(num); Problem 2: Removing all occurrences of a character If we want to remove the occurrences of the letter one at a time, we can write the following function: /** * Function: RemoveAllOccurrences * Usage: s = RemoveAllOccurrences(input, ch); * ------------------------------------ * This function takes a string and returns a corresponding string with all * the occurrences of a given letter removed. It uses a for loop to iterate * through the string, testing each character to see if it matches * the letter to remove, and building the string character by character. */ string RemoveAllOccurrences(string text, char ch)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

09S-Section-Solution - CS106X Winter 2008 Handout 09S...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online