Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9020315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:59:10+00:00 2026-06-16T04:59:10+00:00

This is a task from school, I am supposed to write a recursive function

  • 0

This is a task from school, I am supposed to write a recursive function that will convert a given int to a string, I know I’m close but I can’t point the missing thing in my code, hints are welcome.

void intToStr(unsigned int num, char s[])
{
    if (num < 10)
    {   
        s[0] = '0' + num;
    }

    else
    {
        intToStr(num/10, s);
        s[strlen(s)] = '0' + num%10;
    }
}

Edit: my problem is that the function only works for pre initialized arrays, but if I let the function work on an uninitialized function it will not work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:59:11+00:00Added an answer on June 16, 2026 at 4:59 am

    Unless your array is zero-initialized, you are forgetting to append a null terminator when you modify it.

    Just add it right after the last character:

    void intToStr(unsigned int num, char s[])
    {
        if (num < 10)
        {   
            s[0] = '0' + num;
            s[1] = 0;
        }
    
        else
        {
            intToStr(num/10, s);
            s[strlen(s)+1] = 0; //you have to do this operation here, before you overwrite the null terminator
            s[strlen(s)] = '0' + num%10;
        }
    }
    

    Also, your function is assuming that s has enough space to hold all the digits, so you better make sure it does (INT_MAX is 10 digits long I think, so you need at least 11 characters).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would anyone happen to know a trick that will keep this MSBuild task from
I have this task from school, and I am confuse and lost on how
Ok, I am back on this task. I have my XML properly download from
I have this Maven task to generate Java classes from an XSD file using
From Jobtracker web UI, I see this column called Failed/Killed Task Attempts. I would
This task was actually given to us as homework, but the requirement in the
Please explain what this task is about? Create a generic linked list class that
I want to call a rake task from a cron job that stores remote
I have to complete this task: I'm given a xml file which is exported
This is another prolog task that I can't solve at this moment. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.