problem with writing intToString recursively

Discussion in 'C' started by nil10000, Dec 31, 2010.

  1. nil10000

    nil10000 New Member

    Joined:
    Dec 31, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    I'm trying to write a function that suppose to recieves a natural number and turn it into a string using a recursion, for example the number 123 will be {'1','2','3'}.
    can someone please tell me why my code isn't working?


    Code:
    void intToStr(unsigned int num, char s[])
    {
    	if(num/10==0)
    	{
    		s[0]=(char)(num+48);
    		s[1]='\0';
    	}
    	else
    	{
    		intToStr(num/10, s);
    		s[strlen(s)]=(char)(num%10+48);
    		s[strlen(s)+1]='\0';
    	}
    }
     
    Last edited by a moderator: Jan 2, 2011
  2. nil10000

    nil10000 New Member

    Joined:
    Dec 31, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Never mind, I realized what I did wrong.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice