+ 1
How could I extract the Middle numbers from a another number?
Example: 1234 Extract: 23 Thanks!!
3 Respostas
0
Carlos Alcantara here is one way:
#include <iostream>
#include <string>
unsigned getMidDigit(unsigned n)
{
std::string num = std::to_string(n);
size_t size = num.size();
if (size < 2)
{
return n;
}
bool odd = (size & 1) == 1;
std::string res("");
size_t start = size / 2;
size_t end = (size / 2) + 1;
if (!odd)
{
start -= 1;
}
res.insert(res.begin(), num.begin() + start, num.begin() + end);
return std::stoi(res);
}
int main()
{
std::cout << getMidDigit(1234) << '\n'; // 23
std::cout << getMidDigit(123) << '\n'; // 2
return 0;
}
https://code.sololearn.com/cT7m1i9817nj/?ref=app
0
Can you please tell what programming language? Thanks
0
Yes, c++