checking if element X is a digit?

This is a discussion on checking if element X is a digit? within the C Programming forums, part of the General Programming Boards category; What i'm trying to do is find the amount of digits in character array. i have a char array = ...

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    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))
    Last edited by Axel; 09-04-2005 at 04:50 AM.

  2. #2
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    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. #3
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    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. #4
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    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. #5
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    if you use function fgets to receive the input, then \0 is added automatically
    Last edited by Antigloss; 09-04-2005 at 06:37 AM.

  6. #6
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    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. #7
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    yep i did use fget :|

    fgets(numbercontents, 10, stdin);

  8. #8
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    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. #9
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    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. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.
    Hope is the first step on the road to disappointment.

  11. #11
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Quote 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. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes. The null character must be encoded as the value 0.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    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. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  15. #15
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    wow i try and help someone out only to get flamed!!! sorry!!!!!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking if an element is in a list
    By mohd in forum C Programming
    Replies: 1
    Last Post: 04-07-2009, 04:57 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Replies: 4
    Last Post: 01-05-2008, 10:30 PM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21