Search code examples
c++stringfunctionbooleanvoid

how to call my boolean function so i can use if else for the cout in c++


that my code

so in the main function I want to call the bool function but I don't know how


Solution

  • You can use the following program:

    #include <iostream>
    using namespace std;
    //forward declare the function
    bool palindrome (string a);
    int main() {
        string a;
        cout<<"Masukkan kata : ";
        cin>> a;
        
        if (palindrome(a) == true)//call the function and check the return value. 
        {
            cout<<"Kata tersebut termasuk palindrome ";
        }
        else
            cout<<"Kata tersebut tidak termasuk palindrome";
    }
    bool palindrome (string a) {
        int b;
        b= a.length();
        if (b == 0)
            return 1;
        else if (a[0] != a[b - 1])
            return 0;
        else
            return palindrome (a.substr(1, b - 2));
    }
    
    

    The output of the above program can be seen here.