Help! Subtracting Strings

Hi all. I have been trying to do a RPN Calculator that can deal with int and strings. My RPN is almost complete...except for the subtracting of strings! Here is my code(well part of it anyways):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// this in operators.cpp
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include "operators.h"

using namespace std;

// overloaded function
// this function is used to substract strings
// eg. abcd cd - would give me ab
void minus (stack<string> &stringStack){
	string str1 = stringStack.top();
	stringStack.pop();

	string str2 = stringStack.top();
	stringStack.pop();

	string temp = str1 + str2;

	// i found out that this method only works if i put char
	// eg. remove(temp.begin(), temp.end(), 'a');
	// Is there any other way to substract 2 strings?
	remove(temp.begin(), temp.end(), str);
        remove(temp.begin(), temp.end(), str);

	stringStack.push(temp);
};


The remove() does not work for strings. I was thinking whether i need to compare the 2 strings 1st before doing anything to them so I will be looking it up too. In the time I was hoping if anybody could help me on this one. I had no problems with the addition of Strings but Im having a major headache with the subtraction of strings :(

Thanks in advance
Last edited on
And btw this was my 1st attempt to substract strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// overloaded function
// this function is used to subtract strings
// eg. abcd cd - would give me ab

void minus (stack<string> &stringStack){
	string str1 = stringStack.top();
	stringStack.pop();

	string str2 = stringStack.top();
	stringStack.pop();

	string temp = str2 - str1;

	stringStack.push(temp);
};


I thought it should work cause it worked for my add function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// overloaded function
// this function is used to add strings
// eg. abc def + would give me abcdef

void add (stack<string> &stringStack){
	string str1 = stringStack.top();
	stringStack.pop();

	string str2 = stringStack.top();
	stringStack.pop();

	string temp = str2 + str1;

	stringStack.push(temp);
};
Before I even attempt to provide a solution, what's the definition of operator- between two strings? What do you want as a result? Use examples.

For instance, let's say I have the strings "abc" and "def" and I apply minus() to them. What is the expected result? What is the expected result if I have "abcabc" and "bc"?
if lets say the user keys in "abcabc bc -" then the ans should be "abca"

and thanks in advance for teaching me :D
What if we had "abcd" and "bc". Should the answer be "ad", or should it be the original string "abcd"?
if we had "abcd" and "bc" the ans should be "ad".
Ok, so your definition of minus() for two strings seems to be: Remove from string1 one and only one piece that equals string2, starting the search from the end of the string.

Is this accurate?
yup that is spot on :)
Then use std::string::find_last_of() to find string2 in string1, and then use std::string::erase() to get rid of the match and obtain the result. This site's reference can help you understand the usage of those methods.
ok will do. I'll post my code here once im done. thanks for the advice :)
Sorry but im stuck. I cant seem to find solution from here

1
2
3
4
5
6
7
8
9
10
11
12
void minus (stack<string> &stringStack){
	size_t found;
	string::iterator it;

	string str2 = stringStack.top();
	stringStack.pop();

	string str1 = stringStack.top();
	stringStack.pop();

	found = str1.find_last_of(str2);
};


i also tried using str1.find(str2); and also str1.substr(str2); but sadly it failed :(
Why does it fail? Your code is OK, only incomplete. Now you need to check if found == std::string::npos or not. If not, a match was found. Complete the code.
Yup it is incomplete. I actually added something after this but since it did not work i did not put it in. I was sure that my codes up to "found = ...." was working. But just wanted to check with you if it was correct. This is what i have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
oid minus (stack<string> &stringStack){
	size_t found;
	string::iterator it;

	string str2 = stringStack.top();
	stringStack.pop();

	string str1 = stringStack.top();
	stringStack.pop();

	found = str1.find_last_of(str2);

	
	if (found != string::npos) { //if a match was found
		// not sure abt this for loop
		for (it = str2.begin(); it < str2.end(); it++) {
			str1.erase(it);

			// my logic here is to delete it character by character
			// so "abc bc -" would mean that it would 1st be "ac" before
			// becoming the final product "a"
		}

	}

	else {
		cout << "No match was found." <<endl;
	}
};
I tried this too:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void minus (stack<string> &stringStack){
	size_t found;
	string::iterator it;

	string str2 = stringStack.top();
	stringStack.pop();

	string str1 = stringStack.top();
	stringStack.pop();

	found = str1.find_last_of(str2);

	
	if (found != string::npos) { //if a match was found
		str1.erase(str2.begin(), str2.end());
	}

	else {
		cout << "No match was found." <<endl;
	}
};
According to http://www.cplusplus.com/reference/string/string/erase/ , there is an overload that takes the position and the size of the chunk to be erased. You have the position in your 'found' variable, and you know the length to erase because it is str2.size(). Just like that.
so instead of str1.erase(str2.begin(), str2.end()); i must do str1.erase(found, str2.size());

i implemented it but it was a little off. I entered "abc bc -" and it gave me "ab"
oh wait my bad. i forgot to do some looping. will be back to post the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while (true) {
		found = str1.find_last_of(str2);

		if (found != string::npos) { //if a match was found
			// According to http://www.cplusplus.com/reference/string/string/erase/ 
			// there is an overload that takes the position and the size of the chunk to be erased.
			// I have the position in my "found", 
			// and i know the length to erase because it is str2.size()
			str1.erase(found, str2.size());
		}

		else {
			break;
		}
	}


If I entered "abc bc -" it gives me "a" which is correct.
However when i enterd "abcd bc -" it still gives me "a".

Which part did I go wrong?
I see. I did a quick test @ ideone.com and discovered unusual behavior. I therefore checked the documentation of find_last_of(). This function finds CHARACTERS, not entire substrings! Soooo, the function to use is rfind(). But I see you added a loop, so you seem to have modified the definition of your minus() function. It no longer deletes ONE AND ONLY ONE occurrence; it deletes ALL occurrences. In this case you don't need to bother yourself with a reverse find. Just use find().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// overloaded function
// this function is used to substract strings
// eg. abc cde - would give me ab
void minus (stack<string> &stringStack){
	size_t found;
	string::iterator it;

	string str2 = stringStack.top();
	stringStack.pop();

	string str1 = stringStack.top();
	stringStack.pop();

	while (true) {
		found = str1.rfind(str2);

		if (found != string::npos) { //if a match was found
			// According to http://www.cplusplus.com/reference/string/string/erase/ 
			// there is an overload that takes the position and the size of the chunk to be erased.
			// I have the position in my "found", 
			// and i know the length to erase because it is str2.size()
			str1.erase(found, str2.size());
		}

		else {
			break;
		}
	}

	stringStack.push(str1);
};


omg it worked! haha thank you! You have been a great help sir. This problem driving me crazy till i couldnt sleep well last night! haha thank you! its been a pleasure learning from you thanks :)

PS: I also found out that find() also works but not to sure whats the difference. will read more abt it thanks
Last edited on
Topic archived. No new replies allowed.