Skip to content

Instantly share code, notes, and snippets.

@MohamedTaha98
Created February 4, 2018 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MohamedTaha98/cd5020e057f322403e83da371eaeb2b4 to your computer and use it in GitHub Desktop.
Save MohamedTaha98/cd5020e057f322403e83da371eaeb2b4 to your computer and use it in GitHub Desktop.
#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