I get 2 strings. I remove all the characters present in both the 2 strings.I count the no. of characters left. How do i do this in C++?

Hi Chound,

I would like to help out but could you clarify your question a bit and post what you've got so far?

Do you mean C string or C++ string?

If you have 2 strings and you "remove all the characters present in both the 2 strings", the no. of character left is effectively zero for both string........ ;)


Jim

Ooops I miss that you wanna solve the problem using C++

use the two functions clear() and length()

string str1 = "Judges live on income";
  string str2 = " fixed";
  
  cout << "before clear: " << str1.length() << endl;
  str1.clear();
  cout << "after clear: " << str1.length() << endl;
  // now do the same thing with str2

I wuzznt clear in my question, sorry. Characters that are pressent in both the strings (C++). Eg. string1=Apple string2=Ball. Now 'A(a)' is common, 'l' is common. Remove them. And count the remaining characters.

I wuzznt clear in my question, sorry. Characters that are pressent in both the strings (C++). Eg. string1=Apple string2=Ball. Now 'A(a)' is common, 'l' is common. Remove them. And count the remaining characters.

Then you have to use the find() and erase() functions!

What are the params for find() and erase()?

What are the params for find() and erase()?

Some stuff with a couple of types. Google for dinkumware and do your own research.

//############ my method ############

Say string1 = "corn", string2 = "chound"

set TargetChar = string1[0]
//so that TargetChar is 'c'

search TargetChar in string2

if found: get position of TargetChar in string2, delete TargetChar from string2
//you should use the string2.find() and then string2.erase() here

delete TargetChar from string1 itself

repeat the above until no common characters could be found

count the number of character in string1 and string2
//use length() in here

print string1 and string2 and their number of character

//############ my method ends ############


So chound, I hope the above could help. Just grab a C++ textbook and probably you could find how to manipulate string and get your task finished. Post again if you encounter difficulties. :D

What are the params for find() and erase()?

Look at the code snippet "Playing around with std::string" here on DaniWeb under:
http://www.daniweb.com/code/snippet117.html
Both erase() and find() are shown. You have to use a few if statements to get your work done.

If you want more than a good exercise for your brain, use two multisets and the set_intersection() function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.