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

  • Home
  • SEARCH
  • 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 1107289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:54:58+00:00 2026-05-17T01:54:58+00:00

Given a string and a number, this function will produce all possible combinations of

  • 0

Given a string and a number, this function will produce all possible combinations of n letters from that string.

i.e. If I pass “abcd” and 3, then I should get the following output:

abc
abd
acd
bcd

This is the code

- (void)viewDidLoad {
    [super viewDidLoad];
    [self expand_combinations:@"abcd" arg2:@"" arg3:3];
}

-(void) expand_combinations: (NSString *) remaining_string arg2:(NSString *)s arg3:(int) remain_depth
{
    if(remain_depth==0)
    {
        printf("%s\n",[s UTF8String]);
        return;
    }

    for(int k=0; k < [remaining_string length]; ++k)
    {
        s = [s stringByAppendingString:[[remaining_string substringFromIndex:k] substringToIndex:1]];
        [self expand_combinations:[remaining_string substringFromIndex:k+1] arg2:s arg3:remain_depth - 1];
    }
    return;
}

Instead this is what it prints out

abc
abcd
abcd
abcd
  • 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-05-17T01:54:59+00:00Added an answer on May 17, 2026 at 1:54 am

    Just a quick and dirty solution (didn’t rename variable names and so on) for your function

    -(void) expand_combinations: (NSString *) remaining_string arg2:(NSString *)s arg3:(int) remain_depth
    {
        NSString *newString = [remaining_string stringByReplacingCharactersInRange:NSMakeRange(remain_depth, 1) withString:@""];
        // NSLog(newString);
    
        if(remain_depth==0)
        {
            printf("%s\n",[s UTF8String]);
            return;
        }
        [self expand_combinations:remaining_string arg2:s arg3:remain_depth - 1];
        return;
    }
    

    Output (I used NSLog)

    2010-09-26 04:27:26.462 Untitled[5417:207] abc
    2010-09-26 04:27:26.462 Untitled[5417:207] abd
    2010-09-26 04:27:26.463 Untitled[5417:207] acd
    2010-09-26 04:27:26.467 Untitled[5417:207] bcd
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Algorithm to generate all possible letter combinations of given string down to 2 letters
I want to create a function that will take a string and an integer
I'm looking for a function that will generate an alphanumeric hash. Given a source
Here is my problem: Create a constructor for a telephone number given a string
Given a floating point number, I'm looking to get a String representation of a
Possible Duplicate: how to rotate the given string to left or right in C?
I've been looking for a way to hash a given string in C# that
How do I create an array in smarty from a given string like 22||33||50
I have used the crypt function in c to encrypt the given string. I
I need to generate a path string from a number (in C) e.g: 53431453

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.