Jump to content

union of two strings A and B

- - - - -

  • Please log in to reply
9 replies to this topic

#1
zemzela

zemzela

    Newbie

  • Members
  • PipPip
  • 29 posts
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?

#2
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
What have you done so far?

#3
zemzela

zemzela

    Newbie

  • Members
  • PipPip
  • 29 posts
...

Edited by zemzela, 12 February 2012 - 08:56 AM.


#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
This line:

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
zemzela

zemzela

    Newbie

  • Members
  • PipPip
  • 29 posts
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
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
It's just a simple 'if' statement. In pseudocode, something like this:

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
zemzela

zemzela

    Newbie

  • Members
  • PipPip
  • 29 posts
...

Edited by zemzela, 12 February 2012 - 08:57 AM.


#8
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Concerning your conditional:

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
zemzela

zemzela

    Newbie

  • Members
  • PipPip
  • 29 posts

	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
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
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