
- 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
C++ program to print unique words in a file
A file is a memory location that stores word streams. In a file, there are various words. In this program, we will find all unique words from the file and print them.
A unique word means the number of occurrences of the word is one in the file.
For example,
Tutorials point is best for programming tutorials.
Here, the word tutorial occurs more than once, hence it is not unique. Rest all words are unique.
Algorithm
To check for unique words in the given file. Using iterator with two variables : data and occurence. Input : File Step 1 : Read each line from the file and follow step 2 Step 2 : check for the occurence of the word in the data structure using interator.data. Step 2.1 : if data matches increase the occurrence by one corresponding to the data. Step 2.2 : if data does not match add new value and set its occurence to one. Step 3: Iterate over the date structure. And check for occurence value of each value. Step 3.1 : If the occerence is equals to 1 then prints the data corresponding it else do nothing.
Example
#include <bits/stdc++.h> using namespace std; int main(){ char filename[] = "test.txt"; ofstream fs("test.txt", ios::trunc); fs << "tutorials point is best for programming tutorials"; fs.close(); fstream fs("test.txt"); map<string, int> mp; string word; while (fs >> word){ if (!mp.count(word)) mp.insert(make_pair(word, 1)); else mp[word]++; } fs.close(); for (map<string, int> :: iterator p = mp.begin(); p != mp.end(); p++){ if (p->second == 1) cout << p->first << endl; } return 0; }
Output
best for is point Programming
- Related Articles
- Java Program to Print all unique words of a String
- Python program to print even length words in a string
- Python Program to print unique values from a list
- C# program to print unique values from a list
- Java program to print unique values from a list
- C++ Program to print unique integer pairs
- Java Program to Print even length words
- Program to make file names unique using Python
- Write a C program to print numbers in words using elseif statements
- Python program to print Possible Words using given characters
- C program to count characters, lines and number of words in a file
- Unique Morse Code Words in Python
- Print Words Vertically in Python
- Print all funny words in a string in C++
- Maximum length product of unique words in JavaScript

Advertisements