-6

我有一个'n'数字。我需要打印相同的前 n - 1 位数字。据我所知,它基本上是除以 10。但这是在不使用任何算术运算符的情况下完成的。我尝试使用ctype中的一些转换,但由于数字的确切长度,即没有。位数不知道,我无法使用相同的。

IE

convert int to string, say 12345 to string

set the last position of it to '\0', in my case last position is not known

//i.e. 1, 2, 3, 4, '\0', assuming string size is 5

convert it back to int.// 1234

是否有任何调整或完全不同的东西?

4

1 回答 1

2

Use sprintf() to convert your int to a string. You'll need a char buffer[SOMESIZE]; to pass into sprintf. Now you have a string, that you can manipulate. strlen() will give you the length of it [1]. The rest should follow what you outlined.

I'll let you figure out exactly how to put the bits together.

[1] Actually, you could use the return value from sprintf as length, since it returns the number of characters produced in the string.

于 2013-03-02T10:48:58.947 回答