
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count of words that are present in all the given sentences in C++
We are given multiple sentences in the form of strings. The goal is to count the number of words that exist in all of the sentences.
Note − words containing all lowercase letters will be considered only
If sentences are −
“ I am learning C language ”
“ learning new things is easy “
“ Kids are learning healthy habits “
Only “learning” exists in all three. So count is 1.
Let us understand with examples
Input − “The clothes were dry”, “All the kids were playing”, “Those were the best days”
Output − Count of words that are present in all the given sentences are − 2
Explanation − Words “the” and “were” are present in all sentences.
Input − “We are going to school”, “If you are willing then continue”, “All these are sold”
Output − Count of words that are present in all the given sentences are − 1
Explanation − Word “are” is present in all sentences.
Approach used in the below program is as follows
In this approach we will first store the words of the first sentence in vector<pair<string, bool>> set.
We will find words in the vector set in all other sentences using a unordered_map<string, bool> check.
Take a vector<string> vec. And initialize it with all strings containing sentences.
Number of sentences will be vec.size().
Function words_sentences(vector<string> vec, int size) takes vector of sentences and size and returns the count of words that are present in all the given sentences
Take the initial count as 0.
Take temporary string str to store individual words in a sentence.
Traverse first sentence stored in vec[0] using while loop.
Inside it using another while loop, extract an individual word in str[] till space is encountered.
Now that we have a word of the first sentence in str, add a pair of (str,true) to the set.
Do this for all words in the sentence stored in vec[0].
The vector set now has pairs of all words of the first sentence with true value.
Traverse vector of sentences from 2nd sentence to last sentence using for loop from j=1 to j<size.
Extract each word from the current sentence in vec[j] and store in str.
Mark these words as true in map check using check[str]=true.
Do this for all words in the current sentence in vec[j].
Using for loop, traverse vector set and for current sentence find if these words in check are also in the set.
Using for loop, traverse vector set again.
If the current word appears in all sentences then set[k].second will be true. If yes increment count.
At the end we will have variable count with words occurring in all sentences.
Return count as result.
Example
#include <bits/stdc++.h> using namespace std; int words_sentences(vector<string> vec, int size){ int count = 0; int i = 0; string str; unordered_map<string, bool> check; vector<pair<string, bool>> set ; pair<string, bool> str_bool; while (i < vec[0].size()){ str = ""; while (i < vec[0].size() && vec[0][i] != ' '){ str += vec[0][i]; i++; } i++; if (str != ""){ str_bool = make_pair(str, true); set.push_back(str_bool); } } for (int j = 1; j < size; j++){ check.clear(); i = 0; while (i < vec[j].size()){ str = ""; while (i < vec[j].size() && vec[j][i] != ' '){ str += vec[j][i]; i++; } i++; if (str != ""){ check[str] = true; } } for(int k = 0; k < set.size(); k++){ if (set[k].second != false && check[set[k].first] == false){ set[k].second = false; } else if (set[k].second != false && check[set[k].first] == true){ check[set[k].first] = false; } } } for (int k = 0; k < set.size(); k++){ if (set[k].second == true){ count++; } } return count; } int main(){ vector<string> vec; vec.push_back("Honesty is the best policy"); vec.push_back("policy varies from company to company"); vec.push_back("Employee should follow the policy of a company"); int size = vec.size(); cout<<"Count of words that are present in all the given sentences are: "<<words_sentences(vec, size); return 0; }
Output
If we run the above code it will generate the following output −
Count of words that are present in all the given sentences are: 1
- Related Articles
- Count words in a given string in C++
- Check if given words are present in a string
- C# program to Count words in a given string
- Count all possible N digit numbers that satisfy the given condition in C++
- Print all valid words that are possible using Characters of Array in C++\n
- Count all prefixes of the given binary array which are divisible by x in C++
- Count all pairs with given XOR in C++
- Count subsets that satisfy the given condition in C++
- Recursively print all sentences that can be formed from list of word lists in C++
- Count the number of vowels occurring in all the substrings of given string in C++
- Elements to be added so that all elements of a range are present in array in C++
- Count all Palindromic Subsequence in a given String in C++
- Count the number of sub-arrays such that the average of elements present in the subarray is greater than that not present in the sub-array in C++
- Count numbers in range that are divisible by all of its non-zero digits in C++
- Count numbers in a range that are divisible by all array elements in C++
