-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortestWord.c
More file actions
40 lines (37 loc) · 773 Bytes
/
shortestWord.c
File metadata and controls
40 lines (37 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Incomplete
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
ssize_t find_short(const char *s);
int main(void)
{
char *s = get_string("String: ");
if (find_short(s) == -1)
{
printf("Program Failed\n");
return 1;
}
else
{
printf("Shortest Word in %s: %d\n", s, (int)find_short(s));
}
return 0;
}
ssize_t find_short(const char *s)
{
int count = 0, small;
for (int i = 0, n = strlen(s); i <= n; i++)
{
if (s[i] == ' ' || s[i] == '\0')
{
small = count < (i - count - 1) ? count : (i - count - 1);
count += i;
}
}
if (small > 0)
{
return small;
}
return -1;
}