Guest User

Untitled

a guest
Jun 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /* Function Declaration*/
  4. int LongestWord(char str[]);
  5.  
  6. /* Main*/
  7. int main(void)
  8. {
  9. printf("This is homework two.\n");
  10. char str1[50], str2[50], str3[50];
  11.  
  12. strcpy(str1, "This is a Sample string.");
  13. strcpy(str2, "This sample has less than 123456789012345 leTTers.");
  14. strcpy (str3, "Is thIs a string? (definitely)");
  15.  
  16. int length;
  17.  
  18. printf("string1: %s\n", str1);
  19. printf("string2: %s\n", str2);
  20. printf("string3: %s\n", str3);
  21.  
  22.  
  23. length=LongestWord(str1);
  24. printf("The longest word in string1 is %d\n", length);
  25.  
  26. length=LongestWord(str2);
  27. printf("The longest word in string2 is %d\n", length);
  28.  
  29. length=LongestWord(str3);
  30. printf("The longest word in string3 is %d\n", length);
  31.  
  32. }
  33.  
  34. /* Function Definition*/
  35. int LongestWord(char str[])
  36. {
  37. int i;
  38. int max=0;
  39. for(i=0; i<50; i++)
  40. {
  41. if(str[i]==' '||'.'||','||'?'||'!')
  42. {
  43. if(max < i)
  44. {
  45. max=i;
  46. }
  47. if(str[i]=='\0')
  48. {
  49. return(max);
  50. }
  51. }
  52. }
  53. }
Add Comment
Please, Sign In to add comment