In this tutorial, we will identify and print out all non-repeating words in two given sentences. Unrepeated words refer to words that appear only once in two sentences, that is, they do not appear repeatedly in another sentence. This task involves analyzing an input sentence, identifying individual words, and comparing between two sentences to find words that appear only once. The output should be a list of all these words. This task can be accomplished through various programming methods, such as using loops, arrays, or dictionaries.
Here are two ways to print out all non-repeating words in two given sentences−
Method 1: Use dictionary
Method 2: Using collections
Using a dictionary, count the number of times each word appears in two phrases. We can then look up the dictionary and print all the words that appear only once. The Dictionary function in C is usually used to output all unique words in two specified sentences. The method involves using a dictionary or hash table data structure to store the frequency of each word in two phrases. We can then iterate through the dictionary and print out terms that appear only once.
Here is the syntax without the actual code, using dictionary methods in C to print all non-repeating words in two given sentences -
Declare a dictionary to store word frequencies
map<string, int> freqDict;
Enter two sentences as strings
string sentence1 = "first sentence"; string sentence2 = "second sentence";
Split sentences into words and insert into dictionary
istringstream iss (sentence1 + " " + sentence2); string word; while (iss >> word) { freqDict[word]++; }
Traverse the dictionary and print unique words
for (const auto& [word, frequency]: freqDict) { if (frequency == 1) { cout << word << " "; } }
In C, this is a trick to use dictionary methods to print step by step all non-duplicate items in two specified sentences -
Step 1 - Create two strings s1 and s2 containing sentences.
Step 2 - Declare an empty unordered map string, int> dict, used to record the frequency of each word in the sentence.
Step 3 − Using C’s string stream class, parse the two phrases to extract words.
Step 4 - For each extracted word, check if it appears in the dictionary. If it is, increase its frequency by one. Otherwise, add it to the dictionary with frequency 1.
Step 5 - After processing both sentences, iterate the dictionary and display all terms with frequency 1. These are words that are not repeated in the two sentences.
Step 6 − The time complexity of this method is O(n),
The Chinese translation ofThis code uses an unordered map to store the frequency of each word in the combined phrase. It then loops through the map, adding each word that appears only once to a vector of non-repeating words. Finally, it publishes non-duplicate words. This example implies that the two sentences are hard-coded into the program rather than entered by the user.
#include <iostream> #include <string> #include <unordered_map> #include <sstream> #include <vector> using namespace std; vector<string> getNonRepeatingWords(string sentence1, string sentence2) { // Combine the two sentences into a single string string combined = sentence1 + " " + sentence2; // Create a map to store the frequency of each word unordered_map<string, int> wordFreq; // Use a string stream to extract each word from the combined string stringstream ss(combined); string word; while (ss >> word) { // Increment the frequency of the word in the map wordFreq[word]++; } // Create a vector to store the non-repeating words vector<string> nonRepeatingWords; for (auto& pair : wordFreq) { if (pair.second == 1) { nonRepeatingWords.push_back(pair.first); } } return nonRepeatingWords; } int main() { string sentence1 = "The quick brown fox jumps over the lazy dog"; string sentence2 = "A quick brown dog jumps over a lazy fox"; vector<string> nonRepeatingWords = getNonRepeatingWords(sentence1, sentence2); // Print the non-repeating words for (auto& word : nonRepeatingWords) { cout << word << " "; } cout << endl; return 0; }
a A the The
This strategy involves using sets to find terms that appear only once in two phrases. We can build term sets for each phrase and then identify the intersection of these sets. Finally, we can iterate over the intersection and output all items that appear only once.
A collection is an associative container that holds different elements in sorted order. We can insert terms from both phrases into the collection and any duplicates will be automatically removed.
certainly! Following is the syntax you can use in Python to print out all non-repeating words in two given sentences −
Define two sentences as strings
sentence1 = "The fox jumps over dog" sentence2 = "A dog jumps over fox"
Split each sentence into a list of words
words1 = sentence1.split() words2 = sentence2.split()
Create a set from these two word lists
set1 = set(words1) set2 = set(words2)
Find unique words through the intersection of sets
Nonrepeating = set1.symmetric_difference(set2)
Print unique words
for word in non-repeating: print(word)
Follow the instructions below to output all non-repeating words in two given sentences using aggregate functions in C -
Step 1 - Create two string variables to store the two sentences.
Step 2 - Using the string flow library, split each sentence into independent words and store them in two separate arrays.
Step 3 - Make two sets, one for each sentence, to store unique words.
Step 4 - Loop through each word array and insert each word into the correct set.
Step 5 - Loop through each set and print out the unique words.
The Chinese translation ofIn this code, we use the string stream library to split each sentence into separate words. We then use two collections, uniqueWords1 and uniqueWords2, to store the unique words in each sentence. Finally, we loop through each set and print out the non-duplicate words.
#include <iostream> #include <string> #include <sstream> #include <set> using namespace std; int main() { string sentence1 = "This is the first sentence."; string sentence2 = "This is the second sentence."; string word; stringstream ss1(sentence1); stringstream ss2(sentence2); set<string> uniqueWords1; set<string> uniqueWords2; while (ss1 >> word) { uniqueWords1.insert(word); } while (ss2 >> word) { uniqueWords2.insert(word); } cout << "Non-repeating words in sentence 1:" << endl; for (const auto& w : uniqueWords1) { if (uniqueWords2.find(w) == uniqueWords2.end()) { cout << w << " "; } } cout << endl; cout << "Non-repeating words in sentence 2:" << endl; for (const auto& w : uniqueWords2) { if (uniqueWords1.find(w) == uniqueWords1.end()) { cout << w << " "; } } cout << endl; return 0; }
Non-repeating words in sentence 1: first Non-repeating words in sentence 2: second
The above is the detailed content of Print out all non-repeating words in two given sentences. For more information, please follow other related articles on the PHP Chinese website!