Question: Write a function search() that receives an array of char type, its size and another character, finds the location (the subscript value) of the character
Write a function search() that receives an array of char type, its size and another character, finds the location (the subscript value) of the character in the array that matches the received character and returns this location number.
The program I have is however it does not match any values to the array and it only outputs -1, What is wrong with it?:
#include
using namespace std;
int search(char arr[], int size, char ch)
{
int b;
for(b = 0; b < size; ++b) {
if(ch==arr[b])
return b;
else if(ch!=arr[b])
{
return -1;
}
}
}
int main()
{
int size=0, i=0;
char arr[size], ch=0;
int x=search(arr, size, ch);
cout<<"Enter size"< cin>>size; cout<<"Enter characters"< for(i=0; i { cin>>arr[i]; } cout<<"Enter character to find subscript"< cin>>ch; cout< return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
