Hi I am trying to write a binary search function to search for a specific character in a character array sorted in ascending order. Whenever I run the code though nothing happens, there are no errors or anything, just nothing happpens until I terminate the program myself. Below is my code.

static int binarySearch(char A[], char key) {
        int f= 0;
        int l= A.length;

        while(f < l) {
          int mid= (l-f)/2;
          int x= A[mid];
          if (x == key) return mid;
          if (x < key) f= mid;
          else l= mid;
        }

        return f;
    }

From what I understand a binary search works by halving the search field until we find the key or the search field is empty, which is what I though my code would do, but I can't see where I've gone wrong.

In a normal binary search you take a section of the array and search halfway between the start and end of that section, then discard one half of the section and take the other half for the next iteration. It looks like you are trying to do that, but instead of being halfway between the start and the end of the section, mid is equal to (l - f) / 2 which is only the halfway point when f is 0. I expect that if you searched for something that will be found with x never greater than key, your search will work properly as written.