Skip to main content
Skip to content

Strings · Intermediate

Word Count Program in Hinglish Code

Count words and how often each one appears using an object.

The program

Edit anything and press Run. The output below was produced by running this exact code.

Expected output

Press Run to try it

Kul shabd: 8chai : 3garam : 2meethi : 1aur : 1samosa : 1

How it works

saaf trims the sentence and kaato(text, " ") splits it on spaces into a list of words, so lambai(words) is the word count.

The tally uses an empty object {} as a notebook. For each word, if ginti[w] is still khali the word has not been seen yet, so it starts at 0; then the count goes up by one. Bracket access lets the word itself be the key.

chabiyan(ginti) returns the keys in the order they were first added, and a final loop prints each word with its count.