0

I am sure this code isn't perfect, but I am new to programming and am trying to work out a challenge for checking if a number is a palindrome or not. I tried writing a bool-type function in the code to return 'true' if the number is a palindrome and 'false' otherwise.

Anyway, jumping to context, I want this code to print 'YES" every time the computer notices a sign of palindrome-ism. The code is compiling successfully, however, it does not output anything after 'What is the integer you wanna check palindromism for?:' even when inputting numbers like '12321', '111', '1234321' (palindromes).

Can anyone help me, and if possible, without changing much of the code tell me ways to achieve what I want to (to prove palindrome-ism)?

#include <cstring>

using namespace std;

bool isPalindrome(int x, string md) {
        int y = md.length() + 1;
        char abz[y];
        for (int i=0; i < md.length()-1; ++i) {
            if (abz[i] == (md.length()-1)-i){
                cout << "YES";
            }
            }
        return true;
        }

int main(){
    int x;
    cout << "What is the integer you wanna check palindromism for?: ";
    cin >> x;
    string md = to_string(x);
    isPalindrome(x, md);
    return 0;
}

Thanks!

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • 1
    check your tags. You've placed your c++ code under a Java tag. – Dan Apr 30 '22 at 19:08
  • 2
    you have initialized an array `abz[y]' and try to access it before assigning anything to it – Kevin Apr 30 '22 at 19:10
  • 2
    Instead of inputting the number to an integer and converting it to a string, you can input it directly as a string. When testing, you only need to go half the length. `for (int i=0; i < md.length() / 2; ++i)`. And then, instead of printing a result for every digit, clear a flag when it doesn't match, and then print the result based on the flag. – Weather Vane Apr 30 '22 at 19:13
  • 3
    Of course you can just [reverse the string](https://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-c) and then compare the reversed string to the original string with `==`. – David Grayson Apr 30 '22 at 19:42

1 Answers1

0

I'm not sure what you're trying to do in isPalindrome. One way to check if a string of size len is palindrome is to compare its i-th and (len-i-1)-th characters for i ranging in [0, len / 2). If they differ at any point the string is not palindrome.

Here's how you may do it:

bool isPalindrome(std::string const& md) {
    if (md.empty()) // Empty strings are not palindrome
        return false;
    
    auto const len = md.size();
    auto const halfLen = len / 2;
    for (std::size_t i = 0; i != halfLen; ++i)
        if (md[i] != md[len - i - 1])
            return false;

    return true;
}

Can anyone help me, and if possible, without changing much of the code tell me ways to achieve what I want to (to prove palindrome-ism)?

Please check the comments I've added in the code:

// Include the correct headers
#include <iostream>
#include <string>

// Don't do this
//using namespace std;

// This just checks if the string is palindrome.
// It does not print anything.
bool isPalindrome(std::string const& md) {
    if (md.empty())
        return false;
    
    auto const len = md.size();
    auto const halfLen = len / 2;
    for (std::size_t i = 0; i != halfLen; ++i)
        if (md[i] != md[len - i - 1])
            return false;

    return true;
}

int main() {
    // No need to parse the number and convert it to string again.
    //int x;

    std::string md;
    std::cout << "What is the integer you wanna check palindromism for?: ";
    // NOTE: you may input any string here: not just integers.
    std::cin >> md;

    std::cout << (isPalindrome(md) ? "YES" : "") << '\n';
//                                   ^ print "YES" or nothing

    return 0;
}

You may also implement isPalindrome with algorithms and iterators like so:

// You'll need these two headers
#include <algorithm>
#include <iterator>

template <typename BidIt>
bool isPalindrome(BidIt first, BidIt last) {
    if (first == last)
        return false;

    auto const halfLength = std::distance(first, last);
    auto const mid = std::next(first, halfLength);
    auto const rFirst = std::make_reverse_iterator(last);
    return std::equal(first, mid, rFirst);
}

bool isPalindrome(std::string const& str) {
    return isPalindrome(std::cbegin(str), std::cend(str));
}

This is basically the same algorithm as above but you can reuse

template <typename BidIt>
bool isPalindrome(BidIt, BidIt);

with more containers than just std::string.

paolo
  • 2,345
  • 1
  • 3
  • 17