Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. // ConsoleApplication4.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #define MAX_WORD_LENGTH 100
  10.  
  11. int main()
  12. {
  13. FILE *fptr;
  14. int argc;
  15. char filename[15];
  16. char str[60];
  17. char *largest = (char*)malloc(MAX_WORD_LENGTH);
  18. char *smallest = (char*)malloc(MAX_WORD_LENGTH);
  19. int smallest_len = MAX_WORD_LENGTH, largest_len = 0;
  20. printf("Enter the file name you want to open:\n");
  21. scanf("%s", filename);
  22. fptr = fopen(filename, "r");
  23. if (fptr == NULL)
  24. {
  25. printf("Cannot open file \n");
  26. exit(0);
  27. }
  28. while (fgets(str, 60, fptr) != NULL)
  29. {
  30. char *temp = strtok(str, " ");
  31. while (temp != NULL)
  32. {
  33. if (strlen(temp) > largest_len)
  34. {
  35. strcpy(largest, temp);
  36. largest_len = strlen(largest);
  37. }
  38. temp = strtok(NULL, " ");
  39. }
  40. }
  41. printf("The largest word in the file is: %s\n", largest);
  42. fclose(fptr);
  43.  
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement