Orla Black
Orla Black

Reputation: 11

Why did my code cannot check if the word is a palindrom?

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";
    }
}

Upvotes: 1

Views: 67

Answers (1)

Yunnosch
Yunnosch

Reputation: 26753

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.

Upvotes: 1

Related Questions