'checking if a string contains numbers
I want to check a passcode if it contain at least 2 numbers. I tried it with a double for loop like this:
for(int i = 0; i <= count; i++)
{
for(int k = 0; k < 10; k++)
{
if(passcode[i] == k)
{
printf("yee\n");
break;
}
}
}
but it doesn't work
I thought for each 'round', k would be a number from 0 to 9, and if it would be equal to a number the user gave, it would (in this case) print something
(passcode is an input from the user and count is the amount of signs given in)
Solution 1:[1]
int sum = 0;
for(int i = 0; i <= count; i++)
{
if (passcode[i] >= '0' && passcode[i] <= '9')
{
sum++
}
}
if (sum >=2)
printf("At least two digits");
As mentioned in some comments, you can also use isdigit(), if you have access to this function.
One other important point (also in the comments): characters and their number meaning might be different: '2' is different from 2: '2' is a character, there are 256 ASCII characters. If you want to verify this with a number, you are generally dealing with the ASCII code of that character (which is 50 for the character '2').
Solution 2:[2]
the digits are just ascii values you need single comparision like.
int len = LENGHT_OF_STRING;
char * s = "the string";
for(int i = 0; i < len; i++){
if(s[i] >= '0' && s[i] <= '9'){
prinf("Found digit.");
}
}
your comparision of k belongs to [0, 10) is not a valid implementaion.
This is doing comparision of ascii value of i'th char to the above range.
Solution 3:[3]
You have another option -- let strpbrk() do the work for you. strpbrk() (in string.h) locates the first occurrence within a string of any of the characters specified in the second accept string. So in the case of digits, your accept string is simply "0123456789". The function returns a pointer to the first occurrence found, so you simply loop twice adding 1 to the returned pointer address before the second iteration, considering any NULL return a failure to find two digits in the string.
A short working example is:
#include <stdio.h>
#include <string.h>
#define DIGITS "0123456789"
#define NDIGITS 2
int main (int argc, char **argv) {
int ndigits = NDIGITS;
char *p = argv[1];
if (argc < 2 ) { /* validate 1 argument given for password */
fprintf (stderr, "error: insufficient input,\n"
"usage: %s password\n", argv[0]);
return 1;
}
while (ndigits--) { /* loop NDIGITS times */
if (!(p = strpbrk (p, DIGITS))) { /* get ptr to digit (or NULL) */
fputs ("error: password lacks 2 digits.\n", stderr);
return 1;
}
p += 1; /* increment pointer by 1 for next search */
}
puts ("password contains at least 2 digits");
}
Example Use/Output
$ ./bin/passwd2digits some2digitpass1
password contains at least 2 digits
or
$ ./bin/passwd2digits some2digitpass
error: password lacks 2 digits.
Let strpbrk() worry about how to find the digits -- all you need to do it use it. man 3 strpbrk
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | hsuecu |
| Solution 3 |
