Task: Compare length of two strings


C++: #include <iostream>
#include <algorithm>
#include <string>
#include <list>

using namespace std;

bool cmp(const string& a, const string& b)
{
    return b.length() < a.length(); 
}

void compareAndReportStringsLength(list<string> listOfStrings)
{
    if (!listOfStrings.empty())
    {
        char Q = '"';
        string has_length(" has length ");
        string predicate_max(" and is the longest string");
        string predicate_min(" and is the shortest string");
        string predicate_ave(" and is neither the longest nor the shortest string");

        list<string> ls(listOfStrings); 
        ls.sort(cmp);
        int max = ls.front().length();
        int min = ls.back().length();

        for (list<string>::iterator s = ls.begin(); s != ls.end(); s++)
        {
            int length = s->length();
            string* predicate;
            if (length == max)
                predicate = &predicate_max;
            else if (length == min)
                predicate = &predicate_min;
            else
                predicate = &predicate_ave;

            cout << Q << *s << Q << has_length << length << *predicate << endl;
        }
    }
}

int main(int argc, char* argv[])
{
    list<string> listOfStrings{ "abcd", "123456789", "abcdef", "1234567" };
    compareAndReportStringsLength(listOfStrings);

    return EXIT_SUCCESS;
}





PHP: <?php


function retrieveStrings()
{
    if (isset($_POST['input'])) {
        $strings = explode("\n", $_POST['input']);
    } else {
        $strings = ['abcd', '123456789', 'abcdef', '1234567'];
    }
    return $strings;
}


function setInput()
{
    echo join("\n", retrieveStrings());
}


function setOutput()
{
    $strings = retrieveStrings();


    $strings = array_map('trim', $strings);
    $strings = array_filter($strings);

    if (!empty($strings)) {
        usort($strings, function ($a, $b) {
            return strlen($b) - strlen($a);
        });
        $max_len = strlen($strings[0]);
        $min_len = strlen($strings[count($strings) - 1]);
        foreach ($strings as $s) {
            $length = strlen($s);
            if ($length == $max_len) {
                $predicate = "is the longest string";
            } elseif ($length == $min_len) {
                $predicate = "is the shortest string";
            } else {
                $predicate = "is neither the longest nor the shortest string";
            }
            echo "$s has length $length and $predicate\n";
        }
    }
}

?>


<!DOCTYPE html>
<html lang="en">

<head>
    <style>
        div {
            margin-top: 4ch;
            margin-bottom: 4ch;
        }

        label {
            display: block;
            margin-bottom: 1ch;
        }

        textarea {
            display: block;
        }

        input {
            display: block;
            margin-top: 4ch;
            margin-bottom: 4ch;
        }
    </style>
</head>


<body>
    <main>
        <form action=<?php echo $_SERVER['SCRIPT_NAME'] ?> method="post" accept-charset="utf-8">
            <div>
                <label for="input">Input:
                </label>
                <textarea rows='20' cols='80' name='input'><?php setInput(); ?></textarea>
                </label>
            </div>
            <input type="submit" value="press to compare strings">
            </input>
            <div>
                <label for="Output">Output:
                </label>
                <textarea rows='20' cols='80' name='output'><?php setOutput(); ?></textarea>
            </div>
        </form>
    </main>
</body>

</html>