-1

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'

octopus
  • 1
  • 1
  • You should also share the error/exception you're having while compiling the code. – Gökhan Uçar Oct 26 '21 at 12:08
  • 1
    If `str` and `ending` are different strings, then your condition can never be true. You are comparing addresses, and different strings will store data in different memory addreses. Also, `//&str is the address` - this comment is wrong. `str` is a *reference* to `std::string`, not an address. – Yksisarvinen Oct 26 '21 at 12:09
  • 1
    Sorry to be blunt. All those online competitions are a terrible way how to learn a language. If you want to learn, grab a [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You clearly have problems with basic constructs, randomly adding symbols, calling functions without reading their docs, or removing statements is not the way how to teach yourself. It will only fester more bad habits. – Quimby Oct 26 '21 at 12:17
  • 1
    The question is not whether the address of the last character of one string is the same as the address of the last character of the other string. – molbdnilo Oct 26 '21 at 12:23
  • If string A ends with string B, then the reverse of string A begins with the reverse of string B. – molbdnilo Oct 26 '21 at 12:25
  • 1
    On an unrelated note, `if (condition) return true; else return false;` --> `return condition;`. – molbdnilo Oct 26 '21 at 12:27

1 Answers1

0

You can use the below shown program for the given question:

#include<iostream>
#include <string>
bool solution(std::string const &str, std::string const &ending)
 {
std::size_t index = str.find(ending);
//std::cout<<"index: "<<index<<std::endl;
 if ( index != std::string::npos && (index == (str.size() - ending.size() )))
  {
return true;
  }
 else
  { 
  return false;
  }
}

int main()
{
    std::string u="He has to give me a new name";
    std::string y="name";
    std::cout<<std::boolalpha<<solution(u,y); //this will print true since the first argument u in this case ends with the second argument y
    return 0;
}

Note that instead of using if and else you could just directly write return condition; where condition is the condition inside the if .

These are some of the mistakes in your program:

Mistake 1

In one of the comment you have written

&str is the address

The above quoted comment is wrong since &str means you are passing the argument by reference instead of by value.

Mistake 2

You are comparing the addresses in the statement:

&str.at(str.length()-1)== &ending.at(ending.length()-1)

This condition will not be satisfied if the passed two std::strings are different. This is a logic error in your program.

You can instead use the program that i gave as a sample.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • C++20 Standard library already contains `std::string::ends_with`, no need to reinvent it. `return true`, `return false` in `if else` seems redundant. – Quimby Oct 26 '21 at 12:33
  • This answer seems incomplete. It doesn't explain the changes or the mistake in the original code. – François Andrieux Oct 26 '21 at 13:06