# Read variable character by character

2

I need to make a program in which you enter a number and see if within that number there is a defined number and according to that number of a different result

For example I have to find if at the beginning of a number `x` are the digits 3 and 7 in that order:

Input: 3792

Output: True

Input: 9264

Output: False

Or if in that same digit `x` , at the end of this appear the number 8 and 2 in that order:

Input: 52682

Output: True

Input: 53819

Output: False

The same if the numbers 7 and 4 are inside the number (Not on the edges):

Input: 147419638

Output: True

Input: 25481548

Output: False

I tried the following code with which I managed to read the first two characters, but I can not do it to read the middle ones or those at the end without having to restrict the amount of characters that the user has to enter:

``````#include <iostream>

using namespace std;

int main()
{
char a[20];

cin>>a;

if (a[0] == '3' && a[1] == '7')
{
} else
{
cout<<"Falso"<<endl;
}
}
``````

asked by Spiwocoal 13.09.2018 в 00:52
source

2

Why do not you try to pass the number to text and do a text search?

``````#include <iostream>
#include <string>

int main()
{
auto numero = std::to_string(147419638);
auto busqueda = std::to_string(47);
auto posicion = numero.find(busqueda);

switch (posicion)
{
case 0:
std::cout << busqueda << " se encuentra al principio de " << numero;
break;

case std::string::npos:
std::cout << "No se encuentra " << busqueda << " en " << numero;
break;

default:
if (posicion == (numero.length() - busqueda.length()))
std::cout << busqueda << " se encuentra al final de " << numero;
else
std::cout << numero << " contiene " << busqueda;
break;
}

return 0;
}
``````

source
0

As you have already answered about how to solve it with character strings, I explain how to solve it by managing only numbers.

To do this by playing with numbers, the logarithm in base 10 is a great ally. The grace of this logarithm is that it allows you to calculate the number of digits of a number:

``````#include <cmath>
#include <iostream>

int main()
{
int numero;
std::cin >> numero;

std::cout << "El numero " << numero
<< " tiene " << static_cast<int>(std::log10(numero))+1
<< " digitos\n";
}
``````

This can be used to extract the initial digits with some ease. For this, knowing the number of digits to filter, it is enough to calculate the power in corresponding base 10 and make a division:

``````numero: 371234
digitos: 6

necesito quedarme con los dos primeros, luego
digitos = digitos - 2 -> digitos = 4

divisor = 10^digitos = 10000
numero / divisor = 37 <<--- Lo que estamos buscando
``````

To keep the last two, just calculate the rest of 100

``````numero: 1234582
numero / 100 = 12345
numero % 100 = 82 <<--- Lo que estamos buscando
``````

And finally, to know if the number has the digits 3 and 7 we must discard the two extremes ... the last digit is easy to discard because it is enough to divide the number by 10. To go through the number we continue dividing between 10 and we keep the rest, watching that the number is greater than or equal to 10 to avoid processing the most significant digit:

``````numero: 147419638
numero / 10 = 14741963 <<--- Digito menos significativo fuera
numero % 10 = 3 <<--- El numero tiene un tres

numero / 10 = 1474196
numero % 10 = 6

numero / 10 = 147419
numero % 10 = 9

numero / 10 = 14741
numero % 10 = 1

numero / 10 = 1474
numero % 10 = 4

numero / 10 = 147
numero % 10 = 7 <<--- El numero tiene un siete

numero / 10 = 14
numero % 10 = 4

numero / 10 = 1 <<--- Menor de 10, paramos para ignorar el digito más significativo
``````

Said with code:

``````int digitos = static_cast<int>(std::log10(numero))+1;
int divisor = std::pow(10,digitos-2);
int primerosDigitos = numero / divisor;

bool cumple = ( primerosDigitos == 37 ); // Comprobamos si numero = 37XXXXX

int ultimosDigitos = numero % 100;
cumple |= ( ultimosDigitos == 82 ); // Comprobamos si numero = XXXX82

// Comprobamos si el numero tiene 3 y 7 y no estan en los extremos
bool tiene3 = false;
bool tiene7 = false;
while( numero>= 10 )
{
numero/= 10;
int digito = numero% 10;
tiene3 |= (digito == 3);
tiene7 |= (digito == 7);
}

cumple |= (tiene3 && tiene7);
std::cout << (cumple ? " Verdadero\n" : " Falso\n");
``````