2 Replies - 287 Views - Last Post: 19 May 2012 - 05:54 PM Rate Topic: -----

#1 LogicError  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 05-November 11

String Manipulation

Posted 19 May 2012 - 03:13 PM

My first program in C++. Having issues that I can't figure out how to get around.

/*Problem: Write a function CensorString that takes two strings as input and returns the first string
with all of the characters that are present in the second removed.*/

#include <iostream>
#include <string>

using namespace std;

string CensorString(string text, string remove);
string CensorCharacter(string text, char remove);

	int main(){
		string a = "the lazy fox jumped over the fence";
		string b = "abc";
		string c = CensorString(a, B)/>;
		cout << c << endl;
	}

string CensorString(string text, string remove){
	string result; 
	for (int i = 0; i < remove.length(); i++){
			result = CensorCharacter(text, remove[i]);
	}
	return result;
}

string CensorCharacter(string text, char remove){
	string result;
	for (int i = 0; i < text.length(); i++){
		if (text[i] != remove)
			result += text[i];
	}
	return result;
}


The program only removes the last character of String b from String a. The issue I believe is in lines 21-22.

I was hoping the string result would take the return string of the method CensorCharacter. In the next pass of the loop, the changed string result would then run through CensorCharacter again taking out the next character and continue. Hoping I conveyed my through process well enough to understand what I am trying to do.

Any ideas how to fix 21-22 so it will run the revised string through each pass of the for loop? I have no idea how to do it. Also, any idea if 21-22 would have worked as I intended in Java?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: String Manipulation

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: String Manipulation

Posted 19 May 2012 - 03:43 PM

View PostLogicError, on 20 May 2012 - 12:13 AM, said:

I was hoping the string result would take the return string of the method CensorCharacter. In the next pass of the loop, the changed string result would then run through CensorCharacter again taking out the next character and continue.


That's what would happen if you passed result as an input into CensorCharacter, but you don't. You always call it with text as its argument and text never changes.

What you should do is you should initialize result to be equal to text and then call CensorCharacter with result as its argument (or get rid of the result variable and use text as the input and the output of the CensorCharacter function instead).
Was This Post Helpful? 1
  • +
  • -

#3 LogicError  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 32
  • Joined: 05-November 11

Re: String Manipulation

Posted 19 May 2012 - 05:54 PM

View Postsepp2k, on 19 May 2012 - 06:43 PM, said:

View PostLogicError, on 20 May 2012 - 12:13 AM, said:

I was hoping the string result would take the return string of the method CensorCharacter. In the next pass of the loop, the changed string result would then run through CensorCharacter again taking out the next character and continue.


That's what would happen if you passed result as an input into CensorCharacter, but you don't. You always call it with text as its argument and text never changes.

What you should do is you should initialize result to be equal to text and then call CensorCharacter with result as its argument (or get rid of the result variable and use text as the input and the output of the CensorCharacter function instead).


Heh, I feel dumb. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1