with
function:
- #include <stdio.h>
- #include <string.h>
- int func(char str[100][100],int start,int end,char key[40])
- {
- while(start <= end)
- {
- int mid = (start+end)/2;
- if (strcmp(key, str[mid])==0)
- {
- printf("key found at the position %d\n", mid+1);
- return 0;
- }
- else if
- (strcmp(key, str[mid])>0)
- {
- end = end;
- start = mid+1;
- }
- else
- {
- start=start;
- end = mid-1;
- }
- }
- return -1;
- }
- int 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;
- if(func(str,start,end,key)==-1)
- printf("Failed\n");
- return 0;
- }
with
recursion:
- #include <stdio.h>
- #include <string.h>
- int recursion(char str[100][100],int start,int end,char key[40])
- {
- if(start>end)
- return -1;
- int mid = (start+end)/2;
- if(strcmp(key, str[mid])==0)
- {
- printf("key found at the position %d\n", mid+1);
- return 0;
- }
- else if(strcmp(key, str[mid])>0)
- {
- recursion(str,mid+1,end,key);
- }
- else
- {
- recursion(str,start,mid-1,key);
- }
- }
- int 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;
- if(recursion(str,start,end,key)==-1)
- printf("Failed\n");
- return 0;
- }
একটি মন্তব্য পোস্ট করুন