Skip to content
Related Articles

Related Articles

Custom Jumble Word Game

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 16 Sep, 2020

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:

  1. Sort the given string.
  2. Open the words.txt file using file handling ifstream to read the file in C++ as:

    ifstream words(“words.txt”);

  3. For each word in the file words.txt sort the word and compare it with the given sorted string.
  4. If both the string matches in the above step then print the current word in the file words.txt.
  5. Close the file after all the words has been checked.

Below is the implementation of the above approach:


// 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
        = 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
// Driver Code
int main()
    // Given string str
    string string = "tac";
    // Function Call
    return 0;


Link to text file: Link

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!