#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int lsearch(char a[], char element, int num)
{
int i;
for (i = 0; i < num; i++)
{
if (element == a[i])
{
return i;
}
}
return -1;
}
void main()
{
int arr[10], search;
int i, n, index;
printf("Enter the number of elements of the array!!");
scanf("%d", &n);
printf("Enter the elements of the array!!");
for (i = 0; i < n; i++)
{
scanf("%c", &arr[i]);
}
printf("Enter the element to search\n");
scanf("%c", &search);
printf("Your searched element is %c", search);
index = lsearch(arr, search, n);
if (index == -1)
{
printf("\nSorry!! Element not found");
}
else
{
printf("Element found at position %d", index+1);
}
}
Here is my code to implement linear search in an character array but it gives some unusual output like inputting less characters than specified and not inputting the element to be searched for and directly displaying the result "Element not found".
Furthermore, when I tried to print the element to be searched, I saw that it is taking any random element by it's own.
Please help!!