Word and Letter Count

Author: Theodore Odeluga

The source files for this project can be downloaded from here

This is a Node command line project and Node.js can be downloaded from here

Instructions for running a command line program with Node.js can be found here

This solution could easily be the start of a character count feature like the type you see in word processing software. It keeps count of how many words there are in a string and the number of letters per word.

Let’s step through its logic to better understand how it works.

We begin with a text sample and two iterators.

let sample = "Autumn is my favourite season ";
let i;
let k = -1; //Traverse (all) of the string's characters

The iterator i runs the length of the sample string and k serves the dual purpose of counting spaces to signify the start and end of words as well as the non-spaces to count the characters.

Next up are the variables word_number_id and countletters.

//Track word + letter count

let word_number_id = 1;
let countletters = 0;

While the job of countletters speaks for itself, word_number_id is the variable which actually keeps track of the update for the word count. At the very end of the function this variable is incremented each time countletters is reset to zero.

splitit as the name implies, breaks the string up.

After this, using i to cover the length of splitit, a loop runs to progress k through the broken up string.

Two conditionals test k for equality either to an empty space (to denote the start and end of a word) or a non-space to count a character.

As mentioned earlier, word_number_id completes the operation with its own count.

Here’s the entire program for convenience.

/* Problem: Count the number of letters of each word (and each word)
in a string variable */

let sample = "Autumn is my favourite season ";
let i;
let k = -1; //Traverse (all) of string's characters

//Track word + letter count

let word_number_id = 1;
let countletters = 0;

/* Split characters (for separate counts
(empty spaces + words as separate objects)) */

let splitit = sample.split("");

for(i=0; i < splitit.length; i++){

//Iterate over letters + empty spaces

k++;
splitit[k];

//Count letters (not empty spaces)

if(splitit[k] != " "){
countletters++;
}

//Count empty spaces (not letters)

if(splitit[k] == " "){

console.log("Word " + word_number_id +
" contains " + countletters + " letters ");

/* Reset countletters to zero
if empty space found */

countletters = 0;

//Update word count
word_number_id++;

   }
}

Conclusion

I hope this is something you find useful in your own projects. While various commercial products have long since solved the problem of tracking words and characters in document creation software, there’s nothing like having your own easy to use version within reach whenever you develop such functionality yourself. Feel free to use, modify and extend the code presented here. I hope it helps you develop a cool tool for your next big idea. Happy coding.