+ 1

How could I extract the Middle numbers from a another number?

Example: 1234 Extract: 23 Thanks!!

7th Dec 2020, 6:31 AM
Carlos Alcantara
Carlos Alcantara - avatar
3 Answers
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
7th Dec 2020, 3:09 PM
Flash
0
Can you please tell what programming language? Thanks
7th Dec 2020, 6:37 AM
noteve
noteve - avatar
0
Yes, c++
7th Dec 2020, 6:38 AM
Carlos Alcantara
Carlos Alcantara - avatar