Question
e have a text file named 'lorem_ipsum.txt', which contains words separated by one or more spaces . These words can have different lengths and may
e have a text file named 'lorem_ipsum.txt', which contains words separated byone or more spaces. These words can have different lengths and may contain both uppercase and lowercase characters. The file does not contain any characters other than alphabet characters and spaces, and there is no line break within the file (meaning theresonly one linein the file).
Using the code structure provided in the answer section perform the following tasks:
- Write a function called sortedWordsWithNewDelimiter($str, $del) which:
-
- takes 2 arguments
-
- str for the input string where words are delimited by one or more spaces
- del a delimiter for separating words in the output string
- completes the following tasks:
-
- separate the input string into individual words, make sure empty elements are removed
- convert all words to lowercase
- sort all words into alphabetical order
- output these sorted words as a String, delimited by the supplied input del
- Write another function called lengthOfWordAtPos($str, $del, $pos) which:
-
- takes 3 arguments
-
- str as the input string
- del for the symbol separating the words in str (i.e. the word delimiter)
- pos for the position of the word whose length is used in the functions output
- completes the following tasks:
-
- calculate the number of letters in the word at pos in str
- returns ahuman-readable Stringin like: The length of word 7 is 4 characters
- Read the file named 'lorem_ipsum.txt' into a variable, so it can be manipulated
As an example of the functions use examine the following output:
Heres the code template:
function sortedWordsWithNewDelimiter($str, $del) {
// Complete this function
return ;
}
function lengthOfWordAtPos($str, $del, $pos) {
// Complete this function
return ;
}
?>
// The filename is 'lorem_ipsum.txt'. Read the file contents into $paragraph
$paragraph = ;
?>
The file contains the following contents:
= $paragraph=\"\"?>
Let's use sortedWordsWithNewDelimiter() to process the paragraph with \"-\" as delimiter:
// Use the first function you've just coded
$paragraphSortedWithDash = sortedWordsWithNewDelimiter($paragraph, \"-\");
?>
= $paragraphsortedwithdash=\"\"?>
Let's get the length of the 24th word with lengthOfWordAtPos():
= lengthofwordatpos($paragraphsortedwithdash,=\"\" \"-\",=\"\" 24)=\"\"?>
QUESTION:
Complete the function called sortedWordsWithNewDelimiter()
Heres the code template:
function sortedWordsWithNewDelimiter($str, $del) {
// Complete this function
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started