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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:30:40+00:00 2026-06-02T16:30:40+00:00

I was working on the recursive code to print all possible output strings for

  • 0

I was working on the recursive code to print all possible output strings for an input sequence. For simplification I am going to shorten the problem. I have a String[] array. I want to print all possible combinations of words from [0] to [N] considering only one character from a String at a time. Example: String[] a = {"abc", "def", "ghi"} I should be printing adg, adh, adi, aeg.. etc
Here is my recursive code:

void printLetters(String[] list, int count, String result)
    {
        if(list == null)
            return;
        if(count > list.length-1)
        {
            System.out.println(result);
            return;
        }
        for(int i = 0; i<list[count].length(); i++)
        {
            printLetters(list, count++, result + list[count].charAt(i) + "");
        }
    }

My code is running into an infinite loop as I am getting a StackOverflowError. Can someone point out my mistake?

  • 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-02T16:30:41+00:00Added an answer on June 2, 2026 at 4:30 pm

    The problem is that you are post-incrementing here:

    printLetters(list, count++, result + list[count].charAt(i) + "");
    

    So the recursive call occurs first, then the increment, which practically means you keep calling the method with the same value of count. Use this instead:

    printLetters(list, count + 1, result + list[count].charAt(i) + "");
    

    And are you sure you want to increment count? Because when the recursive call returns you will have count + 1 also in the “parent method”, which I don’t think is what you need.

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

Sidebar

Related Questions

Recursive function for copy of multilevel folder is not working. I have a code
I have the below code which has to do a recursive listing of all
I have been working on a recursive directory scan which scans folders and adds
I have to create a program that finds all the possible ways of filling
Hey all, I was working on a recursive generator to create the fixed integer
I have the following code where I'm trying to get all the properties of
Working with python 2.7. The following code allows me to input the winning percentage
I'm asking this because I'm working with a recursive function that generates a large
So I'm back with another ray tracing question. My code renders spheres all fine
I'm working on some introductory recursion problems and I have a clarifying question I'd

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.