Answer:
Following are the program to this question:
#include <iostream> //defining header file
using namespace std;
int lastNotIn(string str1, string str2) //defining method lastNotIn
{
int s1_len=0, i, loc=0, j=0; //defining integer variable
while(str1[s1_len] != '\0') //defining loop that checks str 1 length
{
s1_len++; // increment length by 1
}
for(i=s1_len-1;i>=0; i--) //reverse loop for str2
{
while (str2[j] != '\0') //finding last index value
{
if (str1[i] == str2[j]) //comparing all character value
{
loc=1; //assign value 1 in loc variable
break; //using break key
}
else //else block
{
loc=0; //assign value 0 to loc variable
}
j++; //increment loop variable value by 1
}
if (loc == 0) //check condition value it is not found then
{
return i; //returns value of i
}
}
return -1; //return value -1
}
int main() //defining main method
{
cout<<lastNotIn("eieioh", "aeiou")<<endl; //call method and print its value
cout<<lastNotIn("aaaiii", "aeiou")<<endl; //call method and print its value
return 0;
}
Output:
5
2
Explanation:
In the above program, a method lastNotIn is defined, that accepts two string value "str1 and str2" in its parameter, inside the method four integer variable "s1_len, i, loc, and j" is declared, in which all the variable assign a value that is equal to 0.
- In the next line, a while loop is declared, that counts str1 value length, and another for loop is used, that reverse value of str2, inside the loop a conditional statement is used, in if the block it will match str1 and str2 value if value match it will assign 0 in loc variable. otherwise, it will go to else block.
- In this block it will assign 0 in loc variable and increment the j value by 1, outside the loop another if block is used, that check value of loc is equal to 0, if it is true, it will return i value.
- In the main method, we call the method two times, the first time, it will give value 5, and the second time, it will give 2.