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;
  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. for (i = 0; i < n; i++)
  24. {
  25. if (testo[i] == ' ' || testo[i] == '\n')
  26. {
  27. if (*max < cnt)
  28. {
  29. *max = cnt;
  30. strncpy(longer, &testo[i - cnt], cnt);
  31. longer[cnt] = '\0';
  32. }
  33. cnt = 0;
  34. }
  35. else
  36. cnt++;
  37. }
  38. }
  39.  
Success #stdin #stdout 0s 10320KB
stdin
Il teso è 123456789 uuuu
stdout
Inserire testo: Hai inserito: Il teso è 123456789 uuuu
La parola piu lunga e' ,lunga 11077 caratteri