-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathanswer.c
41 lines (35 loc) · 1.04 KB
/
answer.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Stuff above not needed for leetcode submission
//------------------------------------------------------------------------------
// Brute Force, probably very inefficient
int strStr(char* haystack, char* needle) {
int hay_length = strlen(haystack);
int needle_length = strlen(needle);
int i;
int j;
int tmp;
// If the needle is an empty string
if (needle_length == 0) return 0;
// Loop through the haystack
for (i = 0; i < hay_length; i++){
// If the first character is a match
if (haystack[i] == needle[0]){
tmp = i;
// Loop through to see if the string matches
for (j = 0; j < needle_length; j++){
if (haystack[tmp] != needle[j]) break;
if (j == (needle_length - 1)) return i;
tmp++;
}
}
}
return -1;
}
//------------------------------------------------------------------------------
// Testing
int main()
{
printf("%d\n", strStr("string", "ring"));
}