Knowee
Questions
Features
Study Tools

Write a C program to Search a Key string using Binary search TechniqueWrite a program to search a key string in the given array of strings using binary search.At the time of execution, the program should print the message on the console as:Enter value of n : For example, if the user gives the input as:Enter value of n : 4Next, the program should print the messages one by one on the console as:Enter string for a[0] : Enter string for a[1] : Enter string for a[2] : Enter string for a[3] : if the user gives the input as:Enter string for a[0] : AppleEnter string for a[1] : OrangeEnter string for a[2] : KiwiEnter string for a[3] : MangoNext, the program should print the message on the console as:Enter key string : if the user gives the input as:Enter key string : Kiwithen the program should print the result as:After sorting the strings in the array areValue of a[0] = AppleValue of a[1] = KiwiValue of a[2] = MangoValue of a[3] = OrangeThe key string Mango is found at the position 2Similarly if the key element is given as Litchi for the above case then the program should print the output as "The key string Litchi is not found in the array".Fill in the missing code so that it produces the desired result.Sample Test CasesTest Case 1:Expected Output:Enter·value·of·n·:·4Enter·string·for·a[0]·:·AppleEnter·string·for·a[1]·:·BananaEnter·string·for·a[2]·:·OrangeEnter·string·for·a[3]·:·MangoEnter·key·string·:·MangoAfter·sorting·the·strings·in·the·array·areValue·of·a[0]·=·AppleValue·of·a[1]·=·BananaValue·of·a[2]·=·MangoValue·of·a[3]·=·OrangeThe·key·string·Mango·is·found·at·the·position·2

Question

Write a C program to Search a Key string using Binary search TechniqueWrite a program to search a key string in the given array of strings using binary search.At the time of execution, the program should print the message on the console as:Enter value of n : For example, if the user gives the input as:Enter value of n : 4Next, the program should print the messages one by one on the console as:Enter string for a[0] : Enter string for a[1] : Enter string for a[2] : Enter string for a[3] : if the user gives the input as:Enter string for a[0] : AppleEnter string for a[1] : OrangeEnter string for a[2] : KiwiEnter string for a[3] : MangoNext, the program should print the message on the console as:Enter key string : if the user gives the input as:Enter key string : Kiwithen the program should print the result as:After sorting the strings in the array areValue of a[0] = AppleValue of a[1] = KiwiValue of a[2] = MangoValue of a[3] = OrangeThe key string Mango is found at the position 2Similarly if the key element is given as Litchi for the above case then the program should print the output as "The key string Litchi is not found in the array".Fill in the missing code so that it produces the desired result.Sample Test CasesTest Case 1:Expected Output:Enter·value·of·n·:·4Enter·string·for·a[0]·:·AppleEnter·string·for·a[1]·:·BananaEnter·string·for·a[2]·:·OrangeEnter·string·for·a[3]·:·MangoEnter·key·string·:·MangoAfter·sorting·the·strings·in·the·array·areValue·of·a[0]·=·AppleValue·of·a[1]·=·BananaValue·of·a[2]·=·MangoValue·of·a[3]·=·OrangeThe·key·string·Mango·is·found·at·the·position·2

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a C program that fulfills your requirements:

#include <stdio.h>
#include <string.h>

void sortStrings(char arr[][20], int n) {
    char temp[20];

    // Sorting strings using bubble sort
    for (int j=0; j<n-1; j++) {
        for (int i=j+1; i<n; i++) {
            if (strcmp(arr[j], arr[i]) > 0) {
                strcpy(temp, arr[j]);
                strcpy(arr[j], arr[i]);
                strcpy(arr[i], temp);
            }
        }
    }
}

int binarySearch(char arr[][20], int n, char* key) {
    int l = 0, r = n - 1;
    while (l <= r) {
        int m = l + (r - l) / 2;

        int res = strcmp(arr[m], key);

        // Check if key is present at mid
        if (res == 0)
            return m;

        // If key greater, ignore left half
        if (res < 0)
            l = m + 1;

        // If key is smaller, ignore right half
        else
            r = m - 1;
    }

    return -1;
}

int main() {
    int n;
    printf("Enter value of n : ");
    scanf("%d", &n);
    char arr[n][20];
    for(int i=0; i<n; i++) {
        printf("Enter string for a[%d] : ", i);
        scanf("%s", arr[i]);
    }

    sortStrings(arr, n);

    printf("After sorting the strings in the array are\n");
    for (int i = 0; i < n; i++)
        printf("Value of a[%d] = %s\n", i, arr[i]);

    char key[20];
    printf("Enter key string : ");
    scanf("%s", key);

    int result = binarySearch(arr, n, key);
    if(result != -1)
        printf("The key string %s is found at the position %d\n", key, result);
    else
        printf("The key string %s is not found in the array\n", key);

    return 0;
}

This program first sorts the array of strings using bubble sort. Then it performs a binary search to find the key string. If the key string is found, it prints the position of the key string in the sorted array. If the key string is not found, it prints a message saying so.

This problem has been solved

Similar Questions

Alex wants to create a program to search a target value in a sorted array. The program should input the array size and elements, as well as the target value. Utilizing binary search, it should determine if the target is present, and if so, provide the index. If not, display a message indicating its absence.Assist Alex in completing the program efficiently.Input format :The first line of input consists of an integer N, representing the size of the sorted array.The second line consists of N space-separated integers, the elements of the sorted array in ascending order.The third line consists of an integer target, the value to search for in the array.Output format :If the target is present in the array, print "The target value X is present at index Y", where X is the target element and Y is the index position (index starts from 0).If the target is not present, print "The target value X is not present in the array", where X is the target element.

Write a program to read and store n+ve numbers in array and search a number k provided by the user using a. Linear Searchb. Binary Search

Single File Programming QuestionProblem StatementAlex wants to create a program to search a target value in a sorted array. The program should input the array size and elements, as well as the target value. Utilizing binary search, it should determine if the target is present, and if so, provide the index. If not, display a message indicating its absence.Assist Alex in completing the program efficiently.Input format :The first line of input consists of an integer N, representing the size of the sorted array.The second line consists of N space-separated integers, the elements of the sorted array in ascending order.The third line consists of an integer target, the value to search for in the array.Output format :If the target is present in the array, print "The target value X is present at index Y", where X is the target element and Y is the index position (index starts from 0).If the target is not present, print "The target value X is not present in the array", where X is the target element.Refer to the sample output for formatting specifications.Code constraints :The given test cases will fall under the following constraints:2 ≤ N ≤ 101 ≤ elements ≤ 1000Sample test cases :Input 1 :514 29 37 48 5948Output 1 :The target value 48 is present at index 3Input 2 :10125 137 248 358 489 587 652 741 823 912850Output 2 :The target value 850 is not present in the array

write a program to print integer no, from 1 to n where n is the input from the user using do while loop in c language use getch

Write a program to perform binary search on a list of integers given below, to search for an element input by the user. If it is found display the element along with its position, otherwise display the message "Search element not found".5, 7, 9, 11, 15, 20, 30, 45, 89, 97

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.