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 8951281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:37:37+00:00 2026-06-15T13:37:37+00:00

I am doing online work using CodeLab for C++ and am not sure what’s

  • 0

I am doing online work using CodeLab for C++ and am not sure what’s wrong with my code. Here is the question:

Write a recursive, int-valued function, len, that accepts a string and returns the number of characters in the string.
The length of a string is:
0 if the string is the empty string (“”).
1 more than the length of the rest of the string beyond the first character.

And here’s my code:

int len(string s)
{
  if (s.length()==0)
    return 0;
 else
 {
    return 1+(len(s)-1);
 }
}

It says I have a run-time error.
Any help?

Thanks.

  • 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-15T13:37:39+00:00Added an answer on June 15, 2026 at 1:37 pm

    Well here:

         return 1+(len(s)-1);
    

    The length of the string never decreases. So you will eventually have a stackoverflow because you never hit your base case (s.length() == 0). You need to get a substring where the length of s decreases by 1:

         return 1+(len(s.erase(0,1))); // erases 1 char from beginning then recurses
    

    Hopefully this is purely academic, because std::string has a length method that will run in constant time. (Not to mention erasing from the front of a string is probably horribly inefficient– see the other answers that work with char *)

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

Sidebar

Related Questions

I'm having trouble getting the following code to work correctly. Using an online IEEE-754
I'm not sure why my code isn't doing the TextWrapping properly. It doesn't wrap
I hope this is something straightforward that I'm doing wrong. I saw something online
Doing an ajax get request works as expected using the following code: $.ajax({ type:
Here's my current code. I'm using express and knox, and I don't think I'm
Hosting the video online is not possible nor is using an SD card a
I'm doing some tutorials online, and I'm stuck with an exercise :Write a function
I've written this code using various online sources but I cannot seem to figure
I'm doing an online judge application, so I have a User model, a Problem
I'm doing an online-judge application and I wish to run a ruby script (or

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.