Certainly! You can achieve this by splitting the input string into words, capitalizing each word, and then joining them back into a new string. Here's a simple JavaScript program for this:
function capitalizeWords(inputString) {
// Split the input string into an array of words
const words = inputString.split(' ');
// Capitalize each word
const capitalizedWords = words.map(word => {
// Ensure the word is not an empty string
if (word.length > 0) {
return word.charAt(0).toUpperCase() + word.slice(1);
} else {
return '';
}
});
// Join the capitalized words into a new string
const resultString = capitalizedWords.join(' ');
return resultString;
}
// Example usage:
const inputString = 'hello world! this is a test.';
const result = capitalizeWords(inputString);
console.log(result); // Outputs: 'Hello World! This Is A Test.'
This capitalizeWords function takes an input string, splits it into an array of words, capitalizes each word, and then joins them back into a new string. The example usage demonstrates how to use the function with a sample input string.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Certainly! You can achieve this by splitting the input string into words, capitalizing each word, and then joining them back into a new string. Here's a simple JavaScript program for this:
This capitalizeWords function takes an input string, splits it into an array of words, capitalizes each word, and then joins them back into a new string. The example usage demonstrates how to use the function with a sample input string.