Write a program to search a character in a string using binary search.
- #include <stdio.h>
- #include <string.h>
- void main()
- {
- int i, n, start, end, mid;
- char str[100][100], key[40];
- printf("Please enter the number of string to be stored : ");
- scanf("%d",&n);
- printf("Enter the string in ascending order\n");
- for(i=0; i<=n-1; i++) {
- scanf("%s", &str[i]);
- }
- printf("Enter the string to be searched : ");
- scanf("%s", &key);
- start = 0;
- end = n-1;
- while(start <= end) {
- mid = (start+end)/2;
- if (strcmp(key, str[mid])==0)
- {
- printf("key found at the position %d\n", mid+1);
- exit(0);
- }
- else if
- (strcmp(key, str[mid])>0)
- {
- end = end;
- start = mid+1;
- }
- else {
- start=start;
- end = mid-1;
- }
- }
- printf("Failed\n");
- }
একটি মন্তব্য পোস্ট করুন