编写一个程序,输入整数 n 并打印该数字的字符数(符号除外)

Write a program that inputs an integer n and prints the number of characters (except for the sign) of the number

本文关键字:数字 打印 字符 符号 整数 一个 程序 输入      更新时间:2023-10-16

如何摆脱标点符号? 例如-1975答案是 4,但我的程序将其计为 5。

#include<iostream>
#include<cstring>
using namespace std;
int main() {
string n;
cin>>n;
cout<<n.length();
}

这可能不是最优雅的解决方案,但我会做这样的事情

int returnlength(int x)
{
std::string a = std::to_string(x);
int size= 0; 
a[0] == '-' ? size =  a.size() - 1 : size =  a.size();
return size; 

}