Open In App

Custom Jumble Word Game

Last Updated : 10 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  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”);

  1. For each word in the file words.txt sort the word and compare it with the given sorted string.
  2. If both the string match in the above step then print the current word in the file words.txt.
  3. Close the file after all the words have 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;
}
Java
import java.util.*;
import java.io.*;

// Java program for the above approach
public class Main {

    // Function that sorts the given string
    // and transform a sorted string to uppercase
    public static String sortString(String word) {
        // Transformed to uppercase
        word = word.toUpperCase();

        // Convert to char array, sort and then reconvert to string
        char tempArray[] = word.toCharArray();
        Arrays.sort(tempArray);
        return new String(tempArray);
    }

    // Function that finds the anagram of
    // given string in the given text file
    public static void jumbledString(String jumble) throws FileNotFoundException {
        // Initialize strings
        String checkPerWord = "";
        String userEnteredAfterSorting;

        // Sort the string
        userEnteredAfterSorting = sortString(jumble);

        // Using filehandling Scanner
        // to read the file
        File file = new File("words.txt");
        Scanner words = new Scanner(file);

        // Check each and every word
        // of words.txt(dictionary)
        while (words.hasNextLine()) {
            checkPerWord = words.nextLine();
            String Ch = sortString(checkPerWord);

            // If words matches
            if (Ch.equals(userEnteredAfterSorting)) {
                // Print the word
                System.out.println(checkPerWord);
            }
        }

        // Close the file
        words.close();
    }

    // Driver Code
    public static void main(String[] args) throws FileNotFoundException {
        // Given string str
        String string = "tac";

        // Function Call
        jumbledString(string);
    }
}
Python3
import string

# Function that sorts the given string
# and transform a sorted string to uppercase
def sortString(word):
    # Transformed to uppercase
    word = word.upper()

    # Sort the words
    return ''.join(sorted(word))

# Function that finds the anagram of
# given string in the given text file
def jumbledString(jumble):
    # Initialize strings
    checkPerWord = ""
    userEnteredAfterSorting = ""

    # Sort the string
    userEnteredAfterSorting = sortString(jumble)

    # Using filehandling open
    # to read the file
    with open('words.txt') as words:
        # Check each and every word
        # of words.txt(dictionary)
        for checkPerWord in words:
            Ch = sortString(checkPerWord)

            # If words matches
            if Ch == userEnteredAfterSorting:

                # Print the word
                print(checkPerWord)

# Driver Code
if __name__ == '__main__':
    # Given string str
    string = "tac"

    # Function Call
    jumbledString(string)

# This code is contributed by Aditya Sharma
JavaScript
// JavaScript program for the above approach

// Function that sorts the given string
// and transform a sorted string to uppercase
function sortString(word) {
    // Transformed to uppercase
    word = word.toUpperCase();

    // Sort the words
    word = word.split('').sort().join('');
    return word;
}

// Function that finds the anagram of
// given string in the given text file
async function jumbledString(jumble) {
    // Initialize strings
    let checkPerWord = "";
    let userEnteredAfterSorting;

    // Sort the string
    userEnteredAfterSorting = sortString(jumble);

    // Using filehandling to read the file
    const words = await fetch('words.txt');

    // If file exist
    if (words.ok) {
        const text = await words.text();
        const wordList = text.split('\n');

        // Check each and every word
        // of words.txt(dictionary)
        for (let i = 0; i < wordList.length; i++) {
            checkPerWord = wordList[i];
            let Ch = sortString(checkPerWord);

            // If words matches
            if (Ch == userEnteredAfterSorting) {

                // Print the word
                console.log(checkPerWord);
            }
        }
    }
}

// Driver Code
// Given string str
let string = "tac";

// Function Call
jumbledString(string);

Output: Link to text file: Link



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads