I need to write the function which return the union of two strings in C++.
public string union(string A, string B)
{
code
}
example
string A="Programing"
string B="Program"
union is string C ="Programing"
Could someone help me how to write the code?
9 replies to this topic
#1
Posted 10 February 2012 - 12:01 PM
|
|
|
#2
Posted 10 February 2012 - 12:55 PM
What have you done so far?
#3
Posted 11 February 2012 - 08:40 AM
...
Edited by zemzela, 12 February 2012 - 08:56 AM.
#4
Posted 11 February 2012 - 11:18 AM
This line:
The boolean value 'is_c_in_s3' controls whether the current character in question, s2, gets added to the final string. So, since you're implementing the union operator, do [I]don't want to add the character if it's already been found. Setting 'is_c_in_s3' to false would cause s2[i] [I]not to be inserted into the final string. So, under which conditions should you set 'is_c_in_s3' to false?
Once you figure that out, replace the line I mentioned above with your conditional and you should have it.
cout<<"Figure this portion out yourself."<<endl;needs to be replaced with your code that compares the current character s2, or 'c' as you've called it, with each character in s3.
The boolean value 'is_c_in_s3' controls whether the current character in question, s2, gets added to the final string. So, since you're implementing the union operator, do [I]don't want to add the character if it's already been found. Setting 'is_c_in_s3' to false would cause s2[i] [I]not to be inserted into the final string. So, under which conditions should you set 'is_c_in_s3' to false?
Once you figure that out, replace the line I mentioned above with your conditional and you should have it.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#5
Posted 11 February 2012 - 01:17 PM
Could you help me how to implement that which you write me in my code... If you know please fixed repair my code, it is very important....
#6
Posted 11 February 2012 - 01:58 PM
It's just a simple 'if' statement. In pseudocode, something like this:
Remember, you can instruct it not to copy the character simply by setting that boolean value.
Just implement the above pseudocode and you're done!
if current character 'c' equals j-th character in s3, Don't copy 'c' to the result string. Break from nearest for loop. end if.
Remember, you can instruct it not to copy the character simply by setting that boolean value.
Just implement the above pseudocode and you're done!
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#7
Posted 11 February 2012 - 02:25 PM
...
Edited by zemzela, 12 February 2012 - 08:57 AM.
#8
Posted 11 February 2012 - 03:15 PM
Concerning your conditional:
You placed your conditional in the wrong spot. It was supposed to take the place of this line, as I said in a previous post:
Hint: the above conditional needs to go INSIDE the inner 'for' loop, so you can check it against every character in s3.
You also got rid of your boolean variable, 'is_c_in_s3', and the final if statement, which is not good. Revert back to your previous version and try again.
zemzela said:
if(s3[j]==c){
break;
// s3+=c;Add the element to String 3
}You placed your conditional in the wrong spot. It was supposed to take the place of this line, as I said in a previous post:
zemzela said:
cout<<"Figure this portion out yourself."<<endl;
Hint: the above conditional needs to go INSIDE the inner 'for' loop, so you can check it against every character in s3.
You also got rid of your boolean variable, 'is_c_in_s3', and the final if statement, which is not good. Revert back to your previous version and try again.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#9
Posted 12 February 2012 - 05:43 AM
bool is_c_in_s3=false;
for(int j=0;j<s3.size();j++){
if (c == s3[j]) {
is_c_in_s3=true;
break;
}
}
if(!is_c_in_s3){
s3=s3+c; //Add to String 3
}
}
cout<<s3<<endl;
}
This is the part which was changed. Now the code really works. Another attempt for me, to write this code in c# but without function union. Did you familiar with that? Thanks for your help.
#10
Posted 12 February 2012 - 12:05 PM
More discussion of this topic at this link
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









