Horje
Split a Sentence into Fixed-Length Blocks without Breaking Words in JavaScript

Given a sentence, our task is to split it into blocks of a fixed length without breaking words using JavaScript.

Example:

Input: 
Sentence= "This is a sample sentence to demonstrate splitting into blocks
without breaking words.", blocksize = 15
Output:
Blocks: [ 'This is a', 'sample sentence ', 'to demonstrate', 'splitting into', 'blocks without', 'breaking words.' ]

Below are the approaches to splitting sentences into blocks of a fixed length without breaking words in JavaScript:

Using reduce

In this approach, we use reduce to iterate through the words of the sentence, accumulating them into blocks of the specified length.

Example: The below code example uses reduce to split it into blocks of a fixed length without breaking words.

JavaScript
function splitIntoBlocks(text, size) {
    const words = text.split(' ');

    return words.reduce((blocks, word) => {
        const last = blocks[blocks.length - 1];

        if ((last + ' ' + word).trim().length <= size) {
            blocks[blocks.length - 1] = (last + ' ' + word).trim();
        } else {
            blocks.push(word);
        }

        return blocks;
    }, ['']);
}

let sentence = 
    "This is a sample sentence to demonstrate splitting into blocks without breaking words.";
let blockSize = 15;

let result = splitIntoBlocks(sentence, blockSize);

console.log("Blocks:", result);

Output
Blocks: [
  'This is a',
  'sample sentence',
  'to demonstrate',
  'splitting into',
  'blocks without',
  'breaking words.'
]

Time complexity: O(n)

Auxiliary Space: O(n)

Using split and forEach

In this approach we first split the sentence into words, then use forEach to accumulate words into blocks

Example: The below code example is a practical implementation to split it into blocks of a fixed length without breaking words using split and forEach.

JavaScript
function splitIntoBlocks(text, size) {
    const words = text.split(' ');
    const blocks = [];
    let current = '';

    words.forEach(word => {
        if ((current + ' ' + word).trim().length <= size) {
            current = (current + ' ' + word).trim();
        } else {
            blocks.push(current);
            current = word;
        }
    });

    if (current) {
        blocks.push(current);
    }

    return blocks;
}

// Sample sentence
let sentence = 
    "This is another example sentence to show how to split it into blocks of fixed length.";
let blockSize = 20;

// Call the function and store the result
let result = splitIntoBlocks(sentence, blockSize);

// Output the result
console.log("Blocks:", result);

Output
Blocks: [
  'This is another',
  'example sentence to',
  'show how to split it',
  'into blocks of fixed',
  'length.'
]

Time Complexity: O(n)

Auxiliary Space: O(n)

Using while loop

In this approach, we use a while loop to iterate through the words of the sentence, accumulating them into blocks of the specified length.

Example: The below code example uses a while loop to split the sentence into blocks of a fixed length without breaking words.

JavaScript
function splitIntoBlocks(text, size) {
    const words = text.split(' ');
    const blocks = [];
    let current = '';
    let index = 0;

    while (index < words.length) {
        if ((current + ' ' + words[index]).trim().length <= size) {
            current = (current + ' ' + words[index]).trim();
            index++;
        } else {
            blocks.push(current);
            current = '';
        }
    }

    if (current) {
        blocks.push(current);
    }

    return blocks;
}

// Sample sentence
let sentence = 
    "This approach uses a while loop to split the sentence into blocks without breaking words.";
let blockSize = 25;

// Call the function and store the result
let result = splitIntoBlocks(sentence, blockSize);

// Output the result
console.log("Blocks:", result);

Output
Blocks: [
  'This approach uses a',
  'while loop to split the',
  'sentence into blocks',
  'without breaking words.'
]


Time Complexity: O(n)

Auxiliary Space: O(n)




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
Difference Between _.assign() &amp; _.merge() Method in Lodash Difference Between _.assign() &amp; _.merge() Method in Lodash
JavaScript Program to Delete all Nodes of a Binary Search Tree JavaScript Program to Delete all Nodes of a Binary Search Tree
How to Make a HTTP Request in JavaScript? How to Make a HTTP Request in JavaScript?
How to Check If It Is a Straight Line in JavaScript? How to Check If It Is a Straight Line in JavaScript?
How To Change The Browser To Fullscreen with JavaScript? How To Change The Browser To Fullscreen with JavaScript?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
17