In: Computer Science
Using cs Cstring and c character as arguments in functiuon strfind(cs,c). The function should return the index of wanted letter in string. For example, strfind("hello world", 'o') would return 4. If the character is not found, the funtion returns -1. Please code in c++
#include <iostream>
using namespace std;
int strfind(string cs,char c){
int i=0;
int l=0;
while(cs[i]!='\0'){//finding the length of the string cs
l++;
i++;
}
for(i=0;i<l;i++){//for each character of string cs
if(cs[i]==c)//if character is found return i
return i;
}
return -1;//return -1 if character is not found
}
int main()
{
cout<<strfind("hello world", 'o');
return 0;
}
Screenshots:
The screenshots are attached below for reference.
Please follow them for proper output.
Please upvote my answer .
Thank you.