#include <iostream>
using namespace std;
//if it is found, return the index of the array
//if not fount, return -1
void findInArray(char arr[], char x, int index, int size, int& pos);
int main()
{
char arr[] = "abcdefghijk";
int size = sizeof(arr) / sizeof(char);
int pos;
char x;
cout << "Enter the element you want to find: " ;
cin >> x;
findInArray(arr, x, 0, size, pos);
if ( pos == -1)
cout << x << " is not found in the array!" << endl;
else
cout << x << " is found in the " << pos << "th index of the array!" << endl;
return 0;
}
void findInArray(char arr[], char x, int index, int size, int& pos)
{
if (x == arr[index])
pos = index;
else if (index >= size)
pos = -1;
else
findInArray(arr, x, index+1, size, pos);
}
using namespace std;
//if it is found, return the index of the array
//if not fount, return -1
void findInArray(char arr[], char x, int index, int size, int& pos);
int main()
{
char arr[] = "abcdefghijk";
int size = sizeof(arr) / sizeof(char);
int pos;
char x;
cout << "Enter the element you want to find: " ;
cin >> x;
findInArray(arr, x, 0, size, pos);
if ( pos == -1)
cout << x << " is not found in the array!" << endl;
else
cout << x << " is found in the " << pos << "th index of the array!" << endl;
return 0;
}
void findInArray(char arr[], char x, int index, int size, int& pos)
{
if (x == arr[index])
pos = index;
else if (index >= size)
pos = -1;
else
findInArray(arr, x, index+1, size, pos);
}