#include <iostream>
#include <string>
int countDigitsString(int value) {
// Convert the number to a string
std::string text = std::to_string(value);
// If negative, ignore the leading '-'
if (!text.empty() && text[0] == '-') {
return static_cast<int>(text.size()) - 1;
}
return static_cast<int>(text.size());
}
int main() {
int number = -12345;
int digits = countDigitsString(number);
std::cout << "Number: " << number << "\n";
std::cout << "Digit count (String method): " << digits << "\n";
}
/*
run:
Number: -12345
Digit count (String method): 5
*/