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.
Examples:
Input: str = “tac”
Output:
act
cat
Explanation:
The words can be formed from the given string “tac” are act, cat.Input: str = “atrew”
Output:
tawer
water
wreat
Explanation:
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:
ifstream words(“words.txt”);
- 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:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function that sorts the given string // and transform a sorted string to uppercase string sortString(string word) { // Transformed to uppercase transform(word.begin(), word.end(), word.begin(), ::toupper); // Sort the words sort(word.begin(), word.end()); return word; } // Function that finds the anagram of // given string in the given text file void jumbledString(string jumble) { // Initialize strings string checkPerWord = ""; string userEnteredAfterSorting; // Sort the string userEnteredAfterSorting = sortString(jumble); // Using filehandling ifstream // to read the file ifstream words("words.txt"); // If file exist if (words) { // Check each and every word // of words.txt(dictionary) while (getline(words, checkPerWord)) { string Ch = sortString(checkPerWord); // If words matches if (Ch == userEnteredAfterSorting) { // Print the word cout << checkPerWord << endl; } } // Close the file words.close(); } } // Driver Code int main() { // Given string str string string = "tac"; // Function Call jumbledString(string); return 0; } |
Link to text file: Link
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- Longest Common Prefix using Word by Word Matching
- C program to Replace a word in a text by another given word
- C++ program to read file word by word
- How to Iterate through a String word by word in C++
- C program to find and replace a word in a File by another given word
- Find the word from a given sentence having given word as prefix
- User-defined Custom Exception with class in C++
- Program to create Custom Vector Class in C++
- Sort an Array of dates in ascending order using Custom Comparator
- How to create a custom String class in C++ with basic functionalities
- Print list items containing all characters of a given word
- Recursively print all sentences that can be formed from list of word lists
- Word Break Problem using Backtracking
- Program to find Smallest and Largest Word in a String
- Length Of Last Word in a String
- String containing first letter of every word in a given string with spaces
- Find shortest unique prefix for every word in a given list | Set 2 (Using Sorting)
- Get the first letter of each word in a string using regex in Java
- Second most repeated word in a sequence
- Find the first repeated word in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


