1

The title pretty much describes my question. I cannot recall what is wrong with my code, can you help me with it?

#include <bits/stdc++.h>
using namespace std;

int p(string &s, int f, int l){
    if(s[f] == s[l]){
    return(s, f +1, l-1);       
}
    else{
        return 0;
    }
    if(f-l == 1 || f == l){
        return 1;
    }

}
int main(){
    string s;
    cin >> s;
    int f = 0;
    int l = s.length()-1;
    if(p(s, f, l)){
        cout << "YA";
    }   
    else{
        cout << "BUKAN";
    }
}
Orla Black
  • 11
  • 2
  • 3
    Are you trying to call `p` recursively? – jkb Jun 27 '22 at 04:15
  • `return(s, f +1, l-1);` what do you expect here? It just returns `l-1`. – 273K Jun 27 '22 at 04:16
  • [Why should I not #include ?](https://stackoverflow.com/q/31816095) [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721) [Why should I always enable compiler warnings?](https://stackoverflow.com/q/57842756) – SuperStormer Jun 27 '22 at 04:21
  • Your third `return` in `p()` can never be executed. And if it could, there would also be a path without a `return` through that function. – Yunnosch Jun 27 '22 at 05:35
  • SO is not a debugging service. As a specific question, rather than a question equivalent to "[it doesn't work](//meta.stackexchange.com/q/147616/133817)". State explicitly what you expect to happen and what actually happens. See [ask] and the other articles in the [help] for more. – outis Jun 30 '22 at 04:30

1 Answers1

1

I choose to answer the question in the body, instead of the question in the title (in line with How do I ask and answer homework questions? which I also appy to "competetive programming" questions).

Some things wrong with your code:

  • Your third return in p() can never be executed, because both branchs of the if-then-else structure before already contain a return.
  • And if it could, there would also be a path without a return through that function.
  • What jkb and 273k hint at in the comments return(s, f +1, l-1); is probably a doomed attempt to call p() recursively, returning instead a the result of the , operators and the - calculation.
Yunnosch
  • 26,130
  • 9
  • 42
  • 54