Created
February 4, 2018 17:38
-
-
Save MohamedTaha98/cd5020e057f322403e83da371eaeb2b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <cs50.h> | |
int search(char** students, char* student_name, int len); | |
int main(void) | |
{ | |
char* students[5] = { | |
"Ahmed", | |
"Bebo", | |
"Cedo", | |
"Dedo", | |
"Eno" | |
}; | |
printf("Put student name: "); | |
char* student_name = get_string(); | |
if(search(students, student_name, sizeof(students) / sizeof(char*))) | |
{ | |
printf("%s is sucess\n", student_name); | |
} | |
else | |
{ | |
printf("%s is faild\n", student_name); | |
} | |
} | |
int search(char** students, char* student_name, int len) { | |
int left = 0; | |
int right = len; | |
while (right >= left) { | |
int mid = (left + right) / 2; | |
if (strcmp(students[mid], student_name) == 0) | |
return 1; | |
else if (strcmp(students[mid], student_name) > 0) | |
right = mid - 1; | |
else | |
left = mid + 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment