Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- /* Function Declaration*/
- int LongestWord(char str[]);
- /* Main*/
- int main(void)
- {
- printf("This is homework two.\n");
- char str1[50], str2[50], str3[50];
- strcpy(str1, "This is a Sample string.");
- strcpy(str2, "This sample has less than 123456789012345 leTTers.");
- strcpy (str3, "Is thIs a string? (definitely)");
- int length;
- printf("string1: %s\n", str1);
- printf("string2: %s\n", str2);
- printf("string3: %s\n", str3);
- length=LongestWord(str1);
- printf("The longest word in string1 is %d\n", length);
- length=LongestWord(str2);
- printf("The longest word in string2 is %d\n", length);
- length=LongestWord(str3);
- printf("The longest word in string3 is %d\n", length);
- }
- /* Function Definition*/
- int LongestWord(char str[])
- {
- int i;
- int max=0;
- for(i=0; i<50; i++)
- {
- if(str[i]==' '||'.'||','||'?'||'!')
- {
- if(max < i)
- {
- max=i;
- }
- if(str[i]=='\0')
- {
- return(max);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment