vendredi 6 août 2021

Random Value Shown, String Manipulation [closed]

I wanted to check if a string is palindrome recursively and a random value is printed. I tried to debug but can't find the error. Here is the code:

#include <bits/stdc++.h>

using namespace std;

int checkPalindrome(string myString, int k)
{
    int middle = myString.length();
    middle /= 2;
    int len = myString.length();
    if (k == middle)
        return 1;

    int ok = 1;
    if (myString[k] != myString[len - 1 - k])
        ok = 0;

    if(!ok)
        return 0;

    if (myString[k] == myString[len - 1 - k])
        checkPalindrome(myString, k + 1);
}

int main()
{

    string string = "ana";
    int x = checkPalindrome(string, 0);
    cout << x;

    return 0;
}

The output is 6421856




Aucun commentaire:

Enregistrer un commentaire