Reputation: 829
so I have structure for competitors. I use binary search to search competitors by their surnames. This is the code:
int binary(tekmovalec abc[], int n, char x[20])
{
int start = 0;
int end = n-1;
int a= strlen(x);
while(start <=end)
{
int mid = (start+end)/2;
if(abc[mid].surName[0]== x[0])
{
print(tekmovalci[mid]);
return mid;
}
else if(x[0]<abc[mid].surName[0])
{
end = mid -1;
}
else
{start = mid +1;}
}
return -1;
}
I have a problem, that function checks only first letter of surname and input array, so if surname is Obrien, and user input is "Obrb" it prints Obrien. I dont know how to expand function to check for all letters of user input. Thank you.
Upvotes: 1
Views: 2302
Reputation: 9395
Use strcmp
to compare the strings in the loop.
int res = strcmp(x, abc[mid].surName);
if(!res)
{
print(tekmovalci[mid]);
return mid;
}
else if(res < 0)
{
end = mid -1;
}
else
{start = mid +1;}
Upvotes: 2