Menu
CoddyTech

Final: adapta un pipeline de texto

Lección 47 de 48 del curso PHP para desarrolladores de C de Coddy.

challenge icon

Desafío

Fácil

La entrada es una lista de cadenas. Adapta el programa de referencia fuente proporcionado en esta tarea: abbreviations elimina las cadenas vacías, toma el primer carácter ASCII de cada cadena restante, lo convierte a mayúsculas y las une con puntos. Conserva el orden; devuelve "none" si no hay cadenas no vacías. Toda la entrada es ASCII.

El script principal proporcionado lee un valor JSON de la entrada estándar, comprueba las API públicas nombradas e imprime el resultado. Implementa las declaraciones del módulo sin imprimir; no reemplace el iniciador ni leas la entrada desde un módulo. La entrada satisface los tipos y límites indicados.

Edita Functions.php. main.php es el archivo de entrada bloqueado. Las dependencias usan require_once __DIR__ con un nombre de archivo relativo.

Programa de referencia en C

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
struct Text { const unsigned char *bytes; size_t length; };
struct Result { unsigned char *bytes; size_t length; };
/* Lengths preserve all ASCII bytes, including an embedded zero byte. */
static int abbreviations(const struct Text *words, size_t count, struct Result *out) {
    if (count > (SIZE_MAX - 5) / 2) return 0;
    unsigned char *result = malloc(count * 2 + 5);
    if (result == NULL) return 0;
    size_t length = 0;
    for (size_t i = 0; i < count; ++i) {
        if (words[i].length == 0) continue;
        if (length != 0) result[length++] = '.';
        unsigned char first = words[i].bytes[0];
        if (first >= 'a' && first <= 'z') first = (unsigned char)(first - 'a' + 'A');
        result[length++] = first;
    }
    if (length == 0) { memcpy(result, "none", 4); length = 4; }
    *out = (struct Result){result, length};
    return 1;
}
int main(void) {
    const struct Text words[] = {
        {(const unsigned char *)"red", 3}, {(const unsigned char *)"", 0},
        {(const unsigned char *)"green", 5}, {(const unsigned char *)"blue", 4}
    };
    struct Result result;
    if (!abbreviations(words, 4, &result)) return 1;
    fwrite(result.bytes, 1, result.length, stdout);
    putchar('\n');
    free(result.bytes);
    return ferror(stdout) ? 1 : 0;
}

Pruébalo tú mismo

<?php
declare(strict_types=1);
require_once __DIR__."/Functions.php";
function ensure(bool $ok,string $message):void{if(!$ok)throw new RuntimeException("API: ".$message);}

$input=json_decode(trim(stream_get_contents(STDIN)),true,512,JSON_THROW_ON_ERROR);
ensure(abbreviations(["a","","b"])==="A.B" && abbreviations([])==="none","port pipeline");
echo abbreviations($input),"\n";

Todas las lecciones de PHP para desarrolladores de C

Practica por tu cuenta: Compilador de PHP online