Mirsella Mirsella - 12 days ago 7
C++ Question

Program that checks whether another number is contained within it

I've just recently started to dabble in coding, and I ran into a problem that I haven't been able to solve for days, and the closest thing I've been able to find online is a program checking whether a number contains a specific digit, but that doesn't really apply in my case, I don't think. The problem is to let the user enter two positive numbers and check whether the reverse of the second number is contained within the first one. For example if you enter 654321 and 345, it would say say that it contains it because the reverse of 345 is 543 and 654321 contains that. Here's what I've been trying, but it has been a disaster.

P.S: The variables should stay integer through the program.

#include <iostream>
using namespace std;
bool check(int longer, int shorter)
{
int i = 1;
int rev=0;
int digit;
while (shorter > 0)
{
digit = shorter%10;
rev = rev*10 + digit;
shorter = shorter/10;
}
cout << rev << endl;
bool win=0;
int left = longer / 10; //54321
int right = longer % 10; // 65432
int middle = (longer /10)%10; // 5432
int middle1;
int middle2;
int trueorfalse = 0;
while (left > 0 && right > 0 && middle1 > 0 && middle2 >0)
{
left = longer / 10; //4321 //321
right = longer % 10; //6543 //654
middle1 = middle%10; //543
middle2= middle/10; //432
if (rev == left || rev == right || rev == middle1 || rev == middle2 || rev == middle)
{
win = true;
}
else
{
win = false;
}
}
return win;
}


int main ()
{
int longer;
int shorter;
int winorno;
cout << "Please enter two numbers, first of which is longer: ";
cin >> longer;
cin >> shorter;
winorno = check(longer,shorter);
if (winorno==true)
{
cout << "It works.";
}
else
{
cout << "It doesn't work.";
}

return 0;
}

Answer

The more you overthink the plumbing, the easier it is to stop up the drain. -- Scotty, Star Trek III.

This becomes much easier if you divide this task in two parts:

  1. Reverse the digits in an integer.

  2. Search the second integer for the reversed integer calculated by the first part.

For the first part, assume that n contains the number to reverse.

int modulo=1;
int reversed_n=0;

do
{
     reversed_n = reversed_n * 10 + (n % 10);
     modulo *= 10;
} while ( (n /= 10) != 0);

The end result is if n contained 345, reversed_n will end up with 543, and modulo will be 1000. We'll need modulo for the second part.

The reason the loop is structured this way is intentional. If the original number is 0, we want to wind up with reversed_n also 0, and modulo as 10.

And now, we can take a similar approach to search the second number, called search, whether it contains reversed_n:

for (;;)
{
    if ((search % modulo) == reversed_n)
    {
        std::cout << "Yes" << std::endl;
        return 0;
    }

    if (search < modulo)
        break;

    search /= 10;
}

std::cout << "No" << std::endl;

Complete program:

#include <iostream>

int main()
{
    int search=654321;
    int n=345;

    int modulo=1;
    int reversed_n=0;

    do
    {
        reversed_n = reversed_n * 10 + (n % 10);
        modulo *= 10;
    } while ( (n /= 10) != 0);


    for (;;)
    {
        if ((search % modulo) == reversed_n)
        {
            std::cout << "Yes" << std::endl;
            return 0;
        }

        if (search < modulo)
            break;
        search /= 10;
    }

    std::cout << "No" << std::endl;
    return 0;
}