Thread: checking if element X is a digit?

1. checking if element X is a digit?

What i'm trying to do is find the amount of digits in character array.

i have a char array = numbercontents.

Code:
```char numbercontents[12] = {'1', '2', '3', '4', 'S'};

the above is defined in the main function.```
Code:
```
int isDigit(char *numbercontents)

int length = 0
int count = 0;

while (numbercontents[length] != '\0' )

if (isgraph((int) *numbercontents)) count++; // seems like an integer, increment count
else { length++;} // otherwise just move to the NEXT element in numbercontents.```
^ is defined in isDigit function.

no idea why it's not working :\

i've also tried

if (isdigit((int) *numbercontents))

2. I think my main problem is accessing the array elements,

printf("%c \n", numbercontents[length]);

prints blank results

printf("%c \n", *numbercontents);

works but only the first character is printed.

3. well...
Code:
`char numbercontents[12] = {'1', '2', '3', '4', 'S'};`
should be
Code:
`char numbercontents[12] = {'1', '2', '3', '4', 'S', '\0'};`
and use printf("%s\n", numbercontents); to print the string

4. Hmm that works for a hard coded array but what about if its entered? (i.e. when theres no \0 entered by the user

5. if you use function fgets to receive the input, then \0 is added automatically

6. hmm while (numbercontents[length] != '\0' ) ends up being an endless loop when a string is entered by the user and then when it's placed in a char array. I need a way to find the array length or know when to stop the loop when it reaches the end, java has .length() can't seem to find anything like that in C.

7. yep i did use fget :|

fgets(numbercontents, 10, stdin);

8. see what i've modified.
Code:
```#include <stdio.h>
#include <ctype.h>

int main( void )
{
int length = 0, count = 0;
char numbercontents[12];

fgets( numbercontents, sizeof numbercontents, stdin );

while (numbercontents[length] != '\0' ) {
if ( isdigit( numbercontents[length] ) ) {
count++; // seems like an integer, increment count
}
length++; /* no need for the else */
// otherwise just move to the NEXT element in numbercontents.
}
printf( "%d\n", count );

return 0;
}```

9. yep that did the trick i didn't use the isdigit function correctly and i didn't need the else after the if statement. thanks.

10. Originally Posted by Antigloss
well...
Code:
`char numbercontents[12] = {'1', '2', '3', '4', 'S'};`
should be
Code:
`char numbercontents[12] = {'1', '2', '3', '4', 'S', '\0'};`
and use printf("%s\n", numbercontents); to print the string
The null character is not needed. Any uninitialized elements of an array in which you initialize any element will be set to zero. Thus, everything past the 'S' is by default initialized to zero. (Since there are in fact extra elements at the end. If there weren't, and the 'S' was the last, it wouldn't expand the array automaticly to make room for it, since you're specifying the size of the array.)

So the null character is not needed.

Quzah.

11. Originally Posted by quzah
Any uninitialized elements of an array in which you initialize any element will be set to zero.
Quzah.
But is it guaranteed that \0 is equal to zero??

12. Yes. The null character must be encoded as the value 0.

Quzah.

13. EASY AS POO, GUYS!!! you are making it sound like it is harder than it really is...

Code:
```#include <cstdlib>
#include <iostream>
#include <string.h>

using namespace std;

int IsDigit(char *numbercontents);
int get_charval(char c);

int main()
{
char a_string[] = "lsjgfls093503lkdsgfjs935793";
int g = IsDigit(a_string);
cout << "There are " << g << " digits in a_string" << endl;
system("Pause");
return 0;
}

int IsDigit(char *numbercontents)
{
int the_occour = 0,len = strlen(numbercontents);
int thechar;
for (int x = 0; x<len; x++)
{
thechar = get_charval(numbercontents[x]);
if (thechar >= 48 && thechar <= 57)
the_occour++;
}
return the_occour;
}

int get_charval(char ch)
{
int x = (char) ch;
return x;
}```
as you may notice, this piece of code is written in C++ and that is because i dont know C, i only know C++. but im sure you can EASILY port this piece of code to C. anyways i hope i solved your problem, Axel

14. 1) It's C++, so you're posting on the wrong forum.
2) What's with the hard coded numbers? Bad form.
3) Why a whole function just to cast an integer to a char? Pointless.
4) Antigloss' loop was much cleaner than your code.
5) There's already a standard function to see if a character is a digit, both in C and C++.
6) If you were going to do this in C++, why not just use the string class?
7) Speaking of the string class, 'string' is a standard class name in C++, and since you did in fact use C++, you shouldn't ever name anything 'string'.

Quzah.

15. wow i try and help someone out only to get flamed!!! sorry!!!!!