Sviluppare una function C che, data come parametro di input una stringa che rappresenta un testo in italiano, determina e restituisce come parametri di output la parola di lunghezza minima contenuta nel testo e la sua lunghezza. Nel testo le parole sono separate da un unico spazio.


  1. /**
  2. Sviluppare una function C che, data come parametro di input una stringa che
  3. rappresenta un testo in italiano, determina e restituisce come parametri di output la
  4. parola di lunghezza minima contenuta nel testo e la sua lunghezza. Nel testo le
  5. parole sono separate da un unico spazio.
  6. **/
  7.  
  8. #include <string.h>
  9. #include <stdio.h>
  10.  
  11. char *func(char *text, int *lenght) {
  12.     char *word, *ret, *buff = strdup( text );
  13.     int len, oldlen;
  14.     oldlen = strlen(buff);
  15.  
  16.     while((word = strtok(buff, " "))) {
  17.         buff = NULL; // See strtok man page
  18.         len = strlen(word);
  19.  
  20.         if(len < oldlen) {
  21.             oldlen = len;
  22.             ret = word;
  23.             *lenght = len;
  24.         }
  25.     }
  26.  
  27.     return ret;
  28. }
  29.  
  30. int main(void) {
  31.     char *testo = "ullamcoare laboris nisare utt aliquid ex ea comaamodi consequat";
  32.     int len;
  33.     char *ret = func(testo, &len);
  34.     printf("Parola: %s\nLunga: %d\n",ret, len);
  35.     return 0;
  36. }


Learn More :