Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   problem with writing intToString recursively (http://www.go4expert.com/forums/writing-inttostring-recursively-t24330/)

nil10000 31Dec2010 15:50

problem with writing intToString recursively
 
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';
        }
}


nil10000 31Dec2010 16:02

Re: problem with writing intToString recursively
 
Never mind, I realized what I did wrong.


All times are GMT +5.5. The time now is 15:35.