# Print all the non-repeating words from the two given sentences

Given two strings A and B, the task is to print all the non-repeating words out of the two given sentences.

Examples:

Input: A = “I have a blue pen”, B = “I got a red pen”
Output: have blue got red
Explanation:
The words have, blue, got and red have not been repeated in either the same sentence or another sentence.

Input: A = “I am going to park”, B = “I am in park”
Output: going to in

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to iterate over all the words and check if the word is being repeated or not. Therefore, the following steps can be followed to compute the answer:

1. Concatenate both the string and store it in another string variable.
2. Extract one word from the concatenated string.
3. If that word is present in either A or in B, then print it. Else, continue for remaining words.

Below is the implementation of the above approach:

## C++

 `// C++ program to print all the ` `// non-repeating words from the ` `// two given sentences ` ` `  `#include ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print all the ` `// non-repeating words from the ` `// two given sentences ` `void` `removeRepeating(string s1, string s2) ` `{ ` `    ``// Concatenate the two strings ` `    ``// into one ` `    ``string s3 = s1 + ``" "` `+ s2 + ``" "``; ` `    ``string words = ``""``; ` `    ``int` `i = 0; ` ` `  `    ``// Iterating over the whole ` `    ``// concatenated string ` `    ``for` `(``auto` `x : s3) { ` `        ``if` `(x == ``' '``) { ` ` `  `            ``// Searching for the word in A. ` `            ``// If while searching, we reach ` `            ``// the end of the string, then ` `            ``// the word is not present in ` `            ``// the string ` `            ``if` `(s1.find(words) == string::npos ` `                ``|| s2.find(words) == string::npos) ` `                ``cout << words; ` ` `  `            ``// Initialise word for the ` `            ``// next iteration ` `            ``words = ``""``; ` `        ``} ` `        ``else` `{ ` `            ``words = words + x; ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string s1 = ``"I have go a pen"``; ` `    ``string s2 = ``"I want to go park"``; ` `    ``removeRepeating(s1, s2); ` `    ``return` `0; ` `} `

## Java

 `// Java program to print all the ` `// non-repeating words from the ` `// two given sentences ` `class` `GFG{ ` ` `  `// Function to print all the ` `// non-repeating words from the ` `// two given sentences ` `static` `void` `removeRepeating(String s1, String s2) ` `{ ` `     `  `    ``// Concatenate the two Strings ` `    ``// into one ` `    ``String s3 = s1 + ``" "` `+ s2 + ``" "``; ` `    ``String words = ``""``; ` `    ``int` `i = ``0``; ` ` `  `    ``// Iterating over the whole ` `    ``// concatenated String ` `    ``for``(``char` `x : s3.toCharArray()) ` `    ``{ ` `       ``if` `(x == ``' '``) ` `       ``{ ` `            `  `           ``// Searching for the word in A. ` `           ``// If while searching, we reach ` `           ``// the end of the String, then ` `           ``// the word is not present in ` `           ``// the String ` `           ``if` `(!s1.contains(words) ||  ` `               ``!s2.contains(words)) ` `               ``System.out.print(words); ` `            `  `           ``// Initialise word for the ` `           ``// next iteration ` `           ``words = ``" "``; ` `       ``} ` `       ``else`  `       ``{ ` `           ``words = words + x; ` `       ``} ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``String s1 = ``"I have go a pen"``; ` `    ``String s2 = ``"I want to go park"``; ` `     `  `    ``removeRepeating(s1, s2); ` `} ` `} ` ` `  `// This code is contributed by sapnasingh4991 `

Output:

```have a pen I want to park
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : sapnasingh4991

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.