How to change a number to char*? Like 50 --> "50"

This is a discussion on How to change a number to char*? Like 50 --> "50" within the C Programming forums, part of the General Programming Boards category; How to do it in the simplest method?...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    77

    How to change a number to char*? Like 50 --> "50"

    How to do it in the simplest method?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    sprintf
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    thank Salem!!!

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    I have a problem about precision multiplication. The code I wrote below.
    Code:
    int charToInt(char x)
    { return (int)(x-'0'); }
    
    char intToChar(int x)
    { return (char)(x+'0'); }
    
    void reverse(char *x, int lenX)
    {
    	int i;
    	char t;
    	for (i = 0; i < (int)(lenX/2); i++) {
    		t = x[i];
    		x[i] = x[lenX-i-1];
    		x[lenX-i-1] = t;
    	}
    	for (i = 0; i < lenX; i++)
    		printf("%c", x[i]);
    	printf("\n");
    }
    
    char *precMult(char *a, char *b, int *lenC, int lenA, int lenB)
    {
    	int i, j, lenMax, lenMin;
    	int *tmp;
    	char *c;
    	lenMax = MAX(lenA, lenB);
    	lenMin = MIN(lenA, lenB);
    	tmp = malloc((lenA+lenB+1) * sizeof *tmp);
    	c = malloc((lenA+lenB+1) * sizeof *tmp);
    	reverse(a, lenA);
    	reverse(b, lenB);
    	for (i = 0; i < lenMax; i++)
    		tmp[i] = 0;
    	for (i = 0; i < lenMin; i++) {
    		for (j = 0; j < lenMax; j++) {
    			tmp[i+j] += charToInt(a[j])*charToInt(b[i]);
    			if (tmp[i+j] >= 10) {
    				tmp[i+j+1] += (int)tmp[i+j]/10;
    				tmp[i+j] %= 10;
    			}
    		}
    	}
    	*lenC = lenA+lenB-1;
    	if (tmp[lenA+lenB-1] > 0)
    		(*lenC)++;
    	for (i = *lenC-1; i >= 0; i--)
    		c[i] = intToChar(tmp[i]);
    	free(tmp);
    	return c;
    }
    How to do a action like "a = precMult(b, c, lenB, lenC, &lenA)". a, b, c are array. lenA, lenB, lenC are int.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Umm . . . change the order of the arguments?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    yes, dwks. I wanna change the order of arguments.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You're asking how you do that?
    Code:
    printf("Hello, %s. You are %d years old.\n", name, age);
    ->
    Code:
    printf("Hello, %d-year-old %s.\n", age, name);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program giving errors
    By andy bee in forum C Programming
    Replies: 5
    Last Post: 08-11-2010, 11:38 PM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 10:42 AM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 01:42 PM
  5. Replies: 2
    Last Post: 09-04-2001, 03:12 PM

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