From b5908268b7bd76418ccde2fd8294902b7b497986 Mon Sep 17 00:00:00 2001 From: G-Pereira Date: Thu, 18 Feb 2016 01:14:17 +0000 Subject: [PATCH] Aula 11 - Problema 5 --- Aula_Pratica_11/Problema_5.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/Aula_Pratica_11/Problema_5.c b/Aula_Pratica_11/Problema_5.c index 16d5c2e..cdd2c1e 100644 --- a/Aula_Pratica_11/Problema_5.c +++ b/Aula_Pratica_11/Problema_5.c @@ -1 +1,32 @@ -// TODO do +#include +#include +#include + +int main() { + char c, *max, *p; + int n = 0, l = 0, i = 0; + printf("Frase? "); + max = (char *) malloc(sizeof(char)); + p = (char *) malloc(sizeof(char)); + while (scanf("%c", &c) == 1) { + if (c == ' ' || c == '\n') { + if (strlen(p) > strlen(max)) { + max = (char *) realloc(max, sizeof(char) * strlen(p)); + strcpy(max, p); + } + n++; + i = 0; + if (c == '\n') break; + } + else { + p = (char *) realloc(p, sizeof(char) * (i + 1)); + p[i] = c; + l++; + i++; + } + } + printf("Numero de palavras: %d\n", n); + printf("Palavra maior: %s\n", max); + printf("Comprimento medio: %.1f\n", (float) l / (float) n); + return 0; +} \ No newline at end of file