How to split sentence into blocks of fixed length without breaking words in JavaScript


In the given problem statement we have to split the given sentence into blocks of fixed length without breaking the words of the sentence. And we have to implement the solution in Javascript.

Understanding the Problem

The problem at hand is to split the given sentence into blocks of a fixed length without breaking its words. So we will divide the sentence into substrings of a given length and also we will ensure that the words will remain intact in every block.

For example suppose we have a sentence as "You are reading this article on Tutorials point website" so the output should be [ 'You are reading', 'this article on', 'Tutorials point', 'website' ]. So we have to implement the function to do this task.

Logic for the given Problem

To solve this problem in Javascript we will define a function called splitSentence. The function takes two parameters: first is the sentence which is to be splitted and the required block length. So the input sentence will be splitted into individual words with the help of the 'split' method. The split method will split the sentence at each space character and return the array of words. And we will use an empty array to store the resulting blocks. With the help of the current block we will keep track of the current block which is being formed. And using a loop we will iterate through the words of the array.

Algorithm

Step 1: Create a function to split the sentence of the given input sentence. And the function takes two arguments, the sentence which is to be splitted and the block length.

Step 2: Split each word with the help of the ‘split’ method and store these words in words variable.

Step 3: Create a blank array to store the resulting blocks.

Step 4: To keep track of the current block, create a currentBlock variable and initially set it to an empty string.

Step 5: Go through each word in the words array with the help of a loop. Check whether the current block meets the criteria of the block length for each word.

Step 6: If the words in each iteration are added to the current block and the block length is not reached at maximum, then the word should be appended to the current block.

Step 7: If the length of the block is exceeded in the current block, then we will create a new block with the current word added.

Step 8: When the loop ends, the last block will be added to the blocks array, and the blocks array is returned as the function's output.

Example

 // Function to split the given sentence into block of words
function splitSentence(sentence, blockLength) {
   const words = sentence.split(' ');
   const blocks = [];
   let currentBlock = '';

   for (let i = 0; i < words.length; i++) {
      const word = words[i];
      if (currentBlock.length + word.length <= blockLength) {
         currentBlock += (currentBlock.length > 0 ? ' ' : '') + word;
      } else {
         blocks.push(currentBlock);
         currentBlock = word;
      }
   }
   if (currentBlock.length > 0) {
      blocks.push(currentBlock);
   }

   return blocks;
}
const sentence = "This is a sentence that has to be splitted into blocks of a fixed length";
const blockLength = 15;

const result = splitSentence(sentence, blockLength);
console.log(result);

Output

[
    'This is a',
    'sentence that',
    'has to be',
    'splitted into',
    'blocks of a',
    'fixed length'
]

Complexity

The time complexity for splitting the sentence into blocks of words is O(n), as we have used the split method and the loop. Here both split method and loop takes O(n) time to break the sentence into blocks of words. And suppose the total number of blocks is 'b' so the space complexity is O(n + b).

Conclusion

In our function we have effectively broken the given sentence into a block of the same length without breaking the words. The time complexity and space complexity for the function is O(n) and O(n+b) respectively. In the overall process we have ensured that each word is exact and meaningful.

Updated on: 14-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements