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
#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:
- The function
isDigitsOnly()
loops through each character of the input string using a For loop. isdigit()
checks whether the current character is a digit (0-9).- If any character is not a digit, the function returns
0
, indicating non-digit characters exist. - If all characters are digits, the function returns
1
. - 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
#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:
- The
strspn()
function returns the length of the initial segment ofstr
consisting only of characters in “0123456789”. - We compare the result with
strlen(str)
to check if the entire string consists of digits. - If they are equal, it means all characters are digits, and the function returns
1
. - 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
#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:
- The
strtol()
function tries to convert the string to an integer. - The
endptr
pointer indicates where the conversion stopped. - If
*endptr
is'\0'
, the string contains only digits. - 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:
- Using a loop with
isdigit()
. - Using
strspn()
. - Using
strtol()
and checking non-digit characters.