Anagram Count using HTML CSS and JavaScript (original) (raw)

Last Updated : 28 Apr, 2025

In this article, we will create an Anagram Counting app using HTML, CSS, and JavaScript. Anagrams are the words of rearranged characters or letters in random order. These words contain the same letters. These rearranged words on sorting result in the same words with the same letters used.

Consider the below example to check whether the specific word is an anagram or not with their count:

**Input: forxxorfxdofr
           for
**Output: 3
**Explanation: Anagrams of the word for – for, orf, ofr appear in the text and hence the count is 3.

Here, we will take the input string and input word from the user and count the number of possible anagrams of that word in the string. It will show the number and also which anagrams of the word exist as the output__._

Prerequisites:

The prerequisites of this application are

Approach:

**Example: The below example illustrates to check the Anagram of the string using JavaScript.

HTML `

Count Anagrams

Input String Enter

CSS

body { text-align: center; margin: auto; display: flex; flex-direction: column; }

.root { margin-top: 5%; display: flex; flex-direction: column; width: 30rem; margin: auto; margin-top: 5%; box-shadow: 0 4px 10px rgb(46, 63, 57); background-color: rgb(122, 199, 173); border-radius: 0 10px; padding: 3%; }

.option { text-align: left; display: flex; flex-direction: column; font-size: x-large; padding: 2%; margin: auto; }

.option>span { display: flex; padding: 2%; }

span>label { width: 50%; }

input { width: 50%; font-size: large; }

JavaScript

inputString.value = ""; inputWord.value = ""; nums.innerText = ""; explain.innerText = "";

function fun() { let count = 0; let res = []; str = inputString.value; word = inputWord.value; if (str === "" || word === "") { window.alert("Incorrect input!"); return; }

let n = str.length;
let wordLen = word.length;
srtWord = word.split("").sort().join("");

for (let i = 0; i < n - wordLen + 1; ++i) {
    let sub = str
        .slice(i, i + wordLen)
        .split("")
        .sort()
        .join("");
    if (sub === srtWord) {
        count += 1;
        res.push("'" + str.slice(i, i + wordLen) + "'");
    }
}
let explainres;
if (count === 0) explainres = "none";
else explainres = res.join(",");

nums.innerText =
    "Input string contains " + count + " Anagrams";
explain.innerText =
    "Anagrams of the word '" +
    word +
    "' are: " +
    explainres;

}

`

**Output:

Peek-2023-08-08-11-27