Skip to content

Commit

Permalink
Merge pull request #5160 from kodenook/main
Browse files Browse the repository at this point in the history
Reto #19 - php
  • Loading branch information
kontroldev authored Oct 2, 2023
2 parents a083887 + f8a68e4 commit ebdd973
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Retos/Reto #19 - ANÁLISIS DE TEXTO [Media]/php/kodenook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare (strict_types = 1);

/**
* The `stats` function takes a string as input and returns a string containing various statistics
* about the input text, including the word count, the largest word, the sentence count, and the
* average number of letters per word.
*
* @param string txt The parameter `txt` is a string that represents a text.
*
* @return string a string that contains the following information:
*/
function stats(string $txt): string
{
$txt = explode(' ', $txt);
$largest = '';
$sentences = 0;
$letter_counts = [];

foreach ($txt as $value) {
if (count(str_split($value)) > count(str_split($largest))) {
$largest = $value;
}
if (str_ends_with($value, '.')) {
$sentences++;
}
$value = str_replace('.', '', $value); // Corregir aquí
array_push($letter_counts, count(str_split($value)));
}

$response = 'Words count: ' . count($txt) . PHP_EOL;
$response .= 'Largest word: ' . $largest . PHP_EOL;
$response .= 'Sentences count: ' . $sentences . PHP_EOL;
$response .= 'Average letter: ' . array_sum($letter_counts) / count($letter_counts) . PHP_EOL;

return $response;
}

$sentence = 'Lorem, ipsum dolor sit amet consectetur adipisicing elit. Minus laudantium numquam quam ex velit consequatur in repellendus praesentium ab? Esse dolor voluptates recusandae at, odit necessitatibus provident sint placeat. Recusandae.';
echo stats($sentence);

0 comments on commit ebdd973

Please sign in to comment.