-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblems3.c
More file actions
50 lines (39 loc) · 804 Bytes
/
problems3.c
File metadata and controls
50 lines (39 loc) · 804 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
41
42
43
44
45
46
47
48
49
50
//problems003.c
//3. Longest Substring Without Repeating Characters
#include <stdio.h>
int lengthOfLongestSubstring(char* s) {
int maxlength;
int start, end, index;
if (s[0] == '\0')
return 0;
start = 0;
end = start + 1;
maxlength = 0;
while (*(s+end) != '\0')
{
for (index = start; index < end; index++)
{
if (s[index] == s[end])
{
if (end - start > maxlength)
{
maxlength = end - start;
}
start = index + 1;
break;
}
}
end++;
}
if (end - start > maxlength)
{
maxlength = end - start;
}
return maxlength;
}
int main(int argc, char *argv[])
{
char a[] = "aubxxbacd";
printf("Max length of substring for a is %d.\r\n", lengthOfLongestSubstring(a));
return 0;
}