Home >Backend Development >C++ >Print out all non-repeating words in two given sentences

Print out all non-repeating words in two given sentences

王林
王林forward
2023-09-15 19:01:03716browse

Print out all non-repeating words in two given sentences

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.

method

Here are two ways to print out all non-repeating words in two given sentences−

Method 1: Use dictionary

Method 2: Using collections

Method 1: Use dictionary

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.

grammar

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 << " ";
   }
}

algorithm

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 of

Example 1

is:

Example 1

This 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;
}

Output

a A the The

Method 2: Using collections

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.

grammar

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)

algorithm

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 of

Example 2

is:

Example 2

In 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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete