Calculate number of digits of a decimal number in c

how am I supposed to find the number of digits of decimal numbers in c?
ex. find how many digits are in 123.4567?!

>Solution :

how am I supposed to find the number of digits of decimal numbers in
c? ex. find how many digits are in 123.4567?

Start by converting it to a string.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
    char input[] = "123.456";

    int i, count = 0, len = strlen(input);
    for (i = 0; i < len; i++) {
        if (input[i] == '.') {
            printf("int count %d\n", count);
            continue;
        }
        if (isdigit(input[i])) {
            count++;
            continue;
        }
        // Found a non-digit non-decimal point.
        break;
    }
    printf("all count %d\n", count);
}

Leave a Reply