fork download
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. void LongerWord(char[], int *, char *);
  5.  
  6. int main(void)
  7. {
  8. int lunghezza = 0;
  9. char testo[100], parola[20] = {0};
  10. printf("Inserire testo: ");
  11. fgets(testo, sizeof(testo), stdin);
  12. printf("Hai inserito: ");
  13. puts(testo);
  14. LongerWord(testo, &lunghezza, parola);
  15. printf("La parola piu lunga e' %s, lunga %d caratteri\n", parola, lunghezza);
  16. return 0;
  17.  
  18. }
  19. void LongerWord(char testo[], int *max, char *longer)
  20. {
  21. int i, cnt = 0, n;
  22. n = strlen(testo);
  23. *max = 0;
  24. for (i = 0; i < n; i++)
  25. {
  26. if (testo[i] == ' ' || testo[i] == '\n')
  27. {
  28. if (*max < cnt)
  29. {
  30. *max = cnt;
  31. strncpy(longer, &testo[i - cnt], cnt);
  32. longer[cnt] = '\0';
  33. }
  34. cnt = 0;
  35. }
  36. else
  37. cnt++;
  38. }
  39. }
Success #stdin #stdout 0s 9432KB
stdin
Il testo è 12345678 uuua
stdout
Inserire testo: Hai inserito: Il testo è 12345678 uuua

La parola piu lunga e' 12345678, lunga 8 caratteri