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.
asked Dec 8, 2013 at 11:48
n32303
8291 gold badge12 silver badges25 bronze badges
1 Answer 1
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;}
answered Dec 8, 2013 at 11:58
doptimusprime
9,4416 gold badges58 silver badges94 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-cpp
std::strcmp?std::string, to complete the set.