I have written this code for a question given as follows but i cant seem to find an error in it and it does not compile on online compilers as well as on vs code.If there is a logical error could you tell what it is? There are various solutions to this problem.I just want to know if this approach and logic is valid and if not what mistakes should be avoided so that code can be compiled.
I am also writing my understanding of this code in comment if it is wrong please do correct me. So that i can learn it and avoid making this mistake in future.
question: Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).
code:
#include<iostream>
//using namespace std; if i use this then my next line could be written as (datatype variable)
bool solution(std::string const &str, std::string const &ending)//&str is the address.
{
if ( &str.at(str.length()-1)== &ending.at(ending.length()-1) || ending=="")//reduce the length by one and that will be the value of the last character which can be checked . I have also declared if the ending string is null then what the condition should be.
{
return true;
}
else
{
return false;
}
}
int main()
{
string u="true";
string y="flee";
solution(u,y);
return 0;
}
output(codewar): Expected: true Actual: false output (compiler):'null'