H08S-SectionSolutions1

H08S-SectionSolutions1 - CS106B Winter 07-08 Handout #8S...

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

View Full Document Right Arrow Icon
CS106B Handout #8S Winter 07-08 January 14, 2008 Section Solutions #1 Problem 1: Removing all occurrences of a character If we want to remove the occurrences of the letter one at a time, returning a completely new string at the end, we can write the following function: /* Function: CensorString * Usage: s = CensorString(input, remove); * ---------------------------------------------- * This function takes two strings and returns the first string with * all the occurrences of letters in the second string removed. * It uses a double for loop to iterate through the string testing each * character to see if it matches any of the letters to remove, and * building the resultant string character by character. */ string CensorString1(string text, string remove) { string result = ""; for (int i = 0; i < text.length(); i++) { bool found = false; for (int k = 0; k < remove.length(); k++) { if (text[i] == remove[k]) { found = true; break; } } if(!found) { result += text[i]; } } return result; } We can also do the same thing by using the
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

H08S-SectionSolutions1 - CS106B Winter 07-08 Handout #8S...

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