Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to calculate the string length of positive integer by program?
How to calculate the string length of positive integer by program?
This program can be realized by the following steps:

1, enter a positive integer.

2. Convert an integer into a string (using the sprintf function)

3. Find the length of the string (strlen function), that is, the number of digits of a positive integer.

The code is as follows:

# Contains? & ltstdio.h & gt

# Contains? & ltstring.h & gt

int? Master ()

{

Dragon? num

Charles? str[20];

scanf("%ld ",& ampnum);

sprintf(str," %ld ",num);

printf("%d ",strlen(str));

Return? 0;

} This writing mainly takes into account that there may be 0 before the integer, such as 00090, which is a two-digit number. If you confirm that there is no leading 0, you can simplify the process, omit the process of converting into a string, and get the string directly with gets.