Given a string str, the task is to print all the anagrams of the given string which forms a word that exists in English Dictionary.
Note: For using dictionary words, a text file is used where all the words of the dictionary are stored.
Input: str = “tac”
The words can be formed from the given string “tac” are act, cat.
Input: str = “atrew”
The words can be formed from the given string “atrew” are “tawer”, “water”, “wreat”.
Approach: The idea is to use the concept of File Handling and a text file(say words.txt) that contains all the meaningful words. Below are the steps:
- Sort the given string.
- Open the words.txt file using file handling ifstream to read the file in C++ as:
- For each word in the file words.txt sort the word and compare it with the given sorted string.
- If both the string matches in the above step then print the current word in the file words.txt.
- Close the file after all the words has been checked.
Below is the implementation of the above approach:
Link to text file: Link