forked from embedded2015/quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursive.c
45 lines (39 loc) · 1.02 KB
/
recursive.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
42
43
44
45
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define TARGET 'g'
#define MAX_SIZE 2600
char smallest_character(char str[], char c, int start, int end);
int main()
{
char input[MAX_SIZE];
int size = 0;
FILE *fp = fopen("test.txt", "r");
while(fscanf(fp, "%c", &input[size]) != EOF)
size++;
char r = smallest_character(input, TARGET, 0, size);
printf("Output : %c\n", r);
fclose(fp);
return 0;
}
char smallest_character(char str[], char c, int start, int end)
{
int checkpoint;
checkpoint = (start + end)/2;
char result = '\0';
if ((start + 1) != end) {
if(str[checkpoint] > c)
result = smallest_character(str, c, start, checkpoint);
else if(str[checkpoint] <= c)
result = smallest_character(str, c, checkpoint, end);
} else {
if(str[start] > c)
return str[start];
else if(str[end] > c)
return str[end];
else
return str[0];
}
return result;
}