Thread: Strings - need digits

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    17

    Question Strings - need digits

    Hi

    I am trying to write a code that accpets a string of characters. I need to count the number of digits in that string and return the counted value.

    This is my code
    include <stdio.h>
    #include <ctype.h>

    #define MAX 15

    int digcnt(int c);

    int main( int argc, char *argv[]) {
    char x[MAX];
    int c;

    printf("Enter a string of chars and digits:\n");

    // fgets(x, MAX, stdin);
    while(( c= getchar()) !='\n') {
    c =x[MAX];

    }

    digcnt(c);
    return 0;
    }

    int digcnt(int c) {

    int b;

    for(b=0; b<MAX; b++) {
    if(isdigit(c)){
    b++;
    }
    }
    printf("There are %d digits in the string.\n", b);
    return b;
    }

    Also how do i check if the string (in my case, x[MAX]) contains digits?
    My output is that it gives me the number of MAX as the number of digits.

    All help is appreciated.
    Last edited by s.sidak; 10-26-2010 at 11:34 PM.

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    i think u should use sscanf(x, "%d", &c);
    inside if to check if the c is a digit
    like if ( x[i] >= '0' && x[i] <= '9')
    i'm not sure

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by s.sidak View Post
    Hi

    I am trying to write a code that accpets a string of characters. I need to count the number of digits in that string and return the counted value.
    All help is appreciated.
    Ok, there are a number of problems in the sample provided but most seem to follow the error shown here...
    Code:
    // fgets(x, MAX, stdin);
    while(( c= getchar()) !='\n') {
    c =x[MAX];
    Think about what you are assigning to what... The goal is to build a string with the typed characters in it... is it not? Which is your string and which is your input variable?
    How do you add new characters to the string?

    In C results are always on the left. It's not 6 + 4 = 10 It's 10 = 6 + 4

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Hi,
    a small program to do what you need :

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    
    #define		BUFFER	16
    
    int
    main(int argc, char **argv)
    {
    	int		count = 0;
    	char	my_string[BUFFER];
    	int		i;
    	
    	while (fgets(my_string, BUFFER, stdin)) {
    		for(i = 0; i < BUFFER; i++) {
    			if (isdigit(my_string[i]))
    				count++;
    			if (my_string[i] == '\n')
    				goto end;
    		}
    	}
    end:
    	printf("Digits : %d\n", count);
    	
    	return 0;
    }
    Regards.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by evariste View Post
    Hi,
    a small program to do what you need :
    And how much do you think our friend learned from that?

    I don't mean to be rude but people don't learn when you simply hand them the answers.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    fgets is better than getchar/scanf("%c") stuff.
    You need another variable for count the digits, you use one variable for iteration and count.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define MAX 15
    
    int digcnt(char *x);
    
    int main( int argc, char *argv[]) {
    char x[MAX];
    
    puts("Enter a string of chars and digits:");
    
    fgets(x, MAX, stdin);
    
    digcnt(x);
    return 0;
    }
    
    int digcnt(char *x) {
    
    int b,count=0;
    
    for(b=0; x[b]!='\0'; b++) {
    if(isdigit(x[b])){
    count++;
    }
    }
    printf("There are %d digits in the string.\n", count);
    return count;
    }

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Quote Originally Posted by CommonTater View Post
    And how much do you think our friend learned from that?

    I don't mean to be rude but people don't learn when you simply hand them the answers.
    That's true , i'm sorry.

    Regards.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    Hi all

    Thank You very much for ur replies. Everything is working now, all thanks to your help and by showing me the examples.

    I was able to understand the two codes given.

    Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM