How to check a given string contains only number or not?
Asked
Active
Viewed 5.1k times
11
-
8Go through the string, use `isdigit()`. – Jan 20 '13 at 08:14
-
1@H2CO3: That was an answer not a comment - you might post it as such. – Clifford Jan 20 '13 at 08:38
-
@Clifford OK, if the community won't feel I'm repwhoring (I do, anyway... :P) – Jan 20 '13 at 08:38
-
Just check manual of `ctype.h`. There are many more `is*` function – Shiplu Mokaddim Jan 20 '13 at 08:39
-
I would have answered it if @H2CO3 did not comment it. :P – Shiplu Mokaddim Jan 20 '13 at 08:40
-
Do you want to check that the string comprises only digits 0-9, or that it parses to produce a number with no whitespace etc? – Potatoswatter Jan 20 '13 at 08:52
-
@Potatoswatter: What's the difference between those two choices? I agree that the question may not be clear, but I am not sure your counter-question is any clearer. – Clifford Jan 20 '13 at 10:40
-
@Clifford `" -123.99e+22 "` can be parsed as a number, and contains only a number, but includes whitespace, punctuation, and an alphabetic character. – Potatoswatter Jan 20 '13 at 12:43
-
@Potatoswatter: Good point. A clarification is indeed needed. – Clifford Jan 20 '13 at 14:09
-
Possible duplicate of [Check if a string has only numbers in C?](https://stackoverflow.com/q/41776955/2908724) – bishop Oct 05 '17 at 19:28
-
This is a great question! – Penguin9 Jan 15 '19 at 01:31
2 Answers
24
You can use the isdigit()
macro to check if a character is a number. Using this, you can easily write a function that checks a string for containing numbers only.
#include <ctype.h>
int digits_only(const char *s)
{
while (*s) {
if (isdigit(*s++) == 0) return 0;
}
return 1;
}
Subminiature stylistic sidenote: returns true for the empty string. You might or might not want this behavior.

aioobe
- 413,195
- 112
- 811
- 826
-
2Considering the other answer so far, that was well worth posting after all ;-) – Clifford Jan 20 '13 at 10:38
-
-
2If you don't want to use `ctype.h` and `isdigit()`, just test for `if(*s<'0' || *s>'9')`. The C-standard guarantees that `c - '0'` has the characters numeric value for any character `c` that is a digit. That is true even for Unicode character sets. – con-f-use Apr 19 '16 at 12:46
-
1
-
You could change the algo to consider floats too. For example 2343.34 should also return true. `int digits_only(const char *s) { while (*s) { if(*s == '.') { *s++; continue; } else if ((isdigit(*s++) == 0)) return 0; } return 1; }` – Vendetta V Dec 22 '20 at 16:08
-
`int digits_only(const char *s) { if(*s == '0') return 0; while (*s) { if(*s>='0' && *s<='9'){ s++; } else return 0; } return 1; }` – Max Oct 12 '22 at 17:04
-4
#include<stdio.h>
void main()
{
char str[50];
int i,len = 0,count = 0;
clrscr();
printf("enter any string:: ");
scanf("%s",str);
len = strlen(str);
for(i=0;i<len;i++)
{
if(str[i] >= 48 && str[i] <= 57)
{
count++;
}
}
printf("%d outoff %d numbers in a string",count,len);
getch();
}

shivtej
- 633
- 9
- 18
-
3`void main()` in 2013? Really? "`str[i]>=48 && str[i]<=57`" isn't exactly the right way to do that part either. – Flexo Jan 20 '13 at 09:59
-
1Too much superfluous (and compiler specific) code for others to criticise and down-vote see H2CO3's answer - short and to the point, with no unnecessary extras. Also this counts the number of ASCII digit characters (in a somewhat obfuscated manner); that is not what the question is about. Sure you could test `count != len`, but as a go/no-go test it is sufficient to stop on first non-digit character. The "magic numbers" 48 and 57 could more intuitively be replaced with the caharacter constants `'0'` and `'9'`. – Clifford Jan 20 '13 at 10:32
-
I know my dear friend but i post simple answer for the problem. And i hope this is the best solution to those who beginner of c programming – Programmingcampus.com Jan 20 '13 at 12:11