Seperating Digits?

This is a discussion on Seperating Digits? within the C Programming forums, part of the General Programming Boards category; Basically is it possible to 'split' up digits from let say an integer or float. ie. The number 741 becomes ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    173

    Seperating Digits?

    Basically is it possible to 'split' up digits from let say an integer or float.
    ie. The number 741 becomes 7, 4 and 1 in 3 diff variables?

    I'm not sure if there is a function to do this (since I'm relatively new to C) but any help would be great

  2. #2
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    First, search the forum before ask, I am sure you could have found something.
    Code:
    #include <assert.h>
    #include <stdio.h>
    #include <malloc.h>
    #include <math.h>
    
    void fDigits(int number){	
    	int aux = 10;
    	int size = 1;
    	int* digits;
    	/* counts the number of digits */
    	while(aux<number){
    		size++;
    		aux *= 10;
    	}
    	digits = (int*)malloc(sizeof(int)*size);
    	assert(digits);
    	aux = number;
    	for(int i=size;i>0;i--){
    		digits[i-1] = (int)(aux/pow(10, i-1));
    		aux -= pow(10, i-1)*digits[i-1];
    	}
    	
    	printf("Received: %d\n", number);
    	for(int i=size-1;i>-1;i--)
    		printf("digit %d := %d\n", i, digits[i]);
    }
    
    int main(int argc, char** argv){
    	fDigits(4532);
    	fDigits(9);	
       return 0;
    }
    Nothing more to tell about me...
    Happy day =)

  3. #3
    Jez
    Jez is offline
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    Or you could use sprintf to convert the integer to a string, and then slice it to get the digits.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Oo okies thanks for the help guyz
    I'll try a search next time as well, gustavosserra.

    Btw, wuz dat code meant to compile? 'Cos I got build errors in it when I tried to compile it.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    473
    if you're using gcc you have to link libmath.....gcc -lm -o code code.c

  6. #6
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Quote Originally Posted by nonpuz
    if you're using gcc you have to link libmath.....gcc -lm -o code code.c
    Uhm... I did not know this. I use Dev, that hides this kind of thing.
    Nothing more to tell about me...
    Happy day =)

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    How would I use the sprintf() function to break them up?

    The format is:
    Code:
     int sprintf(char *string, char *format, arg1, arg2);
    Say I have an int called input and a character array to store the 'seperated digits', how should I go about it? I dont' understand how sprintf() works :S

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    char output[20];
    int num=0xffffffff, count;
    sprintf(output, "%u", num);
    for (count=0; output[count]; count++)
      printf("%c ", output[count]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 12:55 PM
  2. reverse a number digits
    By tootoo in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2007, 11:24 AM
  3. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM
  4. Hex digits stored in char array.
    By Kevinmun in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 03:05 PM
  5. Max Digits
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 05:28 AM

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