Check if a String Contains Only Digits in C

In C, we can check if a string contains only digits using various methods such as iterating through characters with a loop, using isdigit() from ctype.h, and utilizing strspn() from string.h. These methods allow us to validate whether a string consists exclusively of numerical digits (0-9).

In this tutorial, we explore multiple approaches to check if a string contains only digits with examples and explanations.


Examples to Check if a String Contains Only Digits

1. Using a for Loop and isdigit() Function

In this example, we will iterate through each character of the string and check if all characters are digits using the isdigit() function from ctype.h.

main.c

</>
Copy
#include <stdio.h>
#include <ctype.h>

int isDigitsOnly(const char *str) {
    for (int i = 0; str[i] != '\0'; i++) {
        if (!isdigit(str[i])) {
            return 0; // Not a digit
        }
    }
    return 1; // All digits
}

int main() {
    char str1[] = "123456";
    char str2[] = "123a56";

    printf("%s: %s\n", str1, isDigitsOnly(str1) ? "Only digits" : "Contains non-digit characters");
    printf("%s: %s\n", str2, isDigitsOnly(str2) ? "Only digits" : "Contains non-digit characters");

    return 0;
}

Explanation:

  1. The function isDigitsOnly() loops through each character of the input string using a For loop.
  2. isdigit() checks whether the current character is a digit (0-9).
  3. If any character is not a digit, the function returns 0, indicating non-digit characters exist.
  4. If all characters are digits, the function returns 1.
  5. The main() function tests the function with two sample strings: one with only digits and another containing a non-digit.

Output:

123456: Only digits
123a56: Contains non-digit characters

2. Using strspn() Function

In this example, we use the strspn() function from string.h to check if the string consists only of digits.

main.c

</>
Copy
#include <stdio.h>
#include <string.h>

int isDigitsOnly(const char *str) {
    return strspn(str, "0123456789") == strlen(str);
}

int main() {
    char str1[] = "987654";
    char str2[] = "98x654";

    printf("%s: %s\n", str1, isDigitsOnly(str1) ? "Only digits" : "Contains non-digit characters");
    printf("%s: %s\n", str2, isDigitsOnly(str2) ? "Only digits" : "Contains non-digit characters");

    return 0;
}

Explanation:

  1. The strspn() function returns the length of the initial segment of str consisting only of characters in “0123456789”.
  2. We compare the result with strlen(str) to check if the entire string consists of digits.
  3. If they are equal, it means all characters are digits, and the function returns 1.
  4. The main() function tests the method with a valid numeric string and another string with a non-digit.

Output:

987654: Only digits
98x654: Contains non-digit characters

3. Using atoi() and Checking for Non-Digits

This method converts the string to an integer using atoi() but also verifies that the string contains only digits.

main.c

</>
Copy
#include <stdio.h>
#include <stdlib.h>

int isDigitsOnly(const char *str) {
    char *endptr;
    strtol(str, &endptr, 10);
    return (*endptr == '\0');
}

int main() {
    char str1[] = "34567";
    char str2[] = "34a67";

    printf("%s: %s\n", str1, isDigitsOnly(str1) ? "Only digits" : "Contains non-digit characters");
    printf("%s: %s\n", str2, isDigitsOnly(str2) ? "Only digits" : "Contains non-digit characters");

    return 0;
}

Explanation:

  1. The strtol() function tries to convert the string to an integer.
  2. The endptr pointer indicates where the conversion stopped.
  3. If *endptr is '\0', the string contains only digits.
  4. If *endptr is not '\0', it means there are non-digit characters.

Output:

34567: Only digits
34a67: Contains non-digit characters

Conclusion

We explored multiple ways to check if a string contains only digits in C:

  1. Using a loop with isdigit().
  2. Using strspn().
  3. Using strtol() and checking non-digit characters.