inherit
27278
0
Aug 3, 2024 9:13:18 GMT -8
Josh
Apple iManiac / eBay Addict
12,347
July 2004
jwd41190
|
Post by Josh on Mar 27, 2011 13:28:20 GMT -8
I am having problems subtracting two strings.
Let's say string1 is: acbf and string2 is: abcde
How do I return the characters that are left? Do I have to use substr?
This should leave just character f. Even though they are not in order those characters where still present in that string.
This is c++ I am doing it in.
|
|
inherit
16846
0
Nov 19, 2012 15:20:20 GMT -8
Chris
3,036
December 2003
cddude
|
Post by Chris on Mar 27, 2011 16:52:40 GMT -8
Do you need efficiency on a large scale (i.e. comparing things at a size of 10k characters)? If so, I'd look into use a hash table ("map" I think in C++. Minimal experience with it). More details on that here under Unit 3: Hashing. The recitation notes might prove more useful than the lecture notes. Also, how do you want to consider duplicates?... That's important Anyways, if you're comparing -very- short strings, it's simplest to just do something like this (Written in pseudo-code/python): for char1 in string1: # Loop over characters of string 1 foundchar = false for char2 in string2: # Same for string 2 if char1 == char2: foundchar = true delete char2 from string2 # This way we don't run into the character again. This depends on how you want to handle duplicates break # Outside the for now... if foundchar == false: print char1 + " is not in string 2"
print "String 2 contains " + string2 + " which are not in string 1" # We deleted chars as we found them, so this will contain only chars not found in string 1.
|
|
inherit
27278
0
Aug 3, 2024 9:13:18 GMT -8
Josh
Apple iManiac / eBay Addict
12,347
July 2004
jwd41190
|
Post by Josh on Mar 27, 2011 18:04:37 GMT -8
Thanks, that gives me some sort of idea. I was thinking most of this I just having problems implementing it. Hopefully I will get it soon.
|
|