122k views
1 vote
Write the following function without using the C++ string class or any functions in the standard library, including strlen(). You may use pointers, pointer arithmetic or array notation.

Write the function firstOfAny().
The function has two parameters (str1, str2), both pointers to the first character in a C-style string.
You should be able to use literals for each argument.
The function searches through str1, trying to find a match for any character inside str2.
Return a pointer to the first character in str1 where this occurs.
Note that if no characters from str2 appear in str1, the pointer will point to the NUL character at the end of str1.

User Vvvvv
by
3.7k points

1 Answer

1 vote

Answer:

See explaination

Step-by-step explanation:

#include<iostream>

using namespace std;

const char* firstOfAny(const char *str1,const char *str2){

const char *p=str1;

while((*p)!='\0'){

const char *q=str2;

while((*q)!='\0'){

if((*p)==(*q)){return p;}

q++;

}

p++;

}

return p;

}

int main(){

cout<<firstOfAny("ZZZZuker","aeiou");

cout<<endl;

cout<<firstOfAny("ZZZzyx","aeiou");

return 0;

}

User Abhimanyuaryan
by
5.0k points