Our server costs ~$56 per month to run. Please consider donating or becoming a Patron to help keep the site running. Help us gain new members by following us on Twitter and liking our page on Facebook!
Current time: July 18, 2025, 4:25 am

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Here's a simple programming problem for you to solve
#35
RE: Here's a simple programming problem for you to solve
My contribution.  I can't be bothered with pure c either, but I wanted to show pool some recursion:

Code:
// ********************* If you're gonna cheat, might as well go for 25 points! ****************

#include <iostream>
#include <string>
using namespace std;

string str;
int digits;
int counter = 0;

// The following appends a series of single characters to whatever comes in-- originally an empty string; so each call will add a new digit //
void printStrings(string curStr, int curDigit){
    for (int i = 0; i < str.length(); i++){
        string newStr = curStr + str[i];
        if (curDigit < digits -1) printStrings (newStr, curDigit +1);  // If we aren't on the last digit, keep recursing (i.e. add another digit) //
        else {                                                         // If we ARE on the last digit, we can just go ahead and print each string //
            counter++;
            cout << endl << newStr;
        }
    }
}

int main()
{
    cout << "Input some unique characters: " ;
    cin >> str ;
    cout << "Max length: ";
    cin >> digits ;
    cout << endl << "Created following strings:" << endl;
    printStrings("", 0);                                              // Starts the recursive routine here
    cout << endl << endl << "(" << counter << " total)";
    return 0;
}

http://cpp.sh/5lfu

PS  It's the one time I'm proud to say: SteelCurtain, mine's smaller than yours! Big Grin
Reply



Messages In This Thread
RE: Here's a simple programming problem for you to solve - by bennyboy - April 28, 2016 at 7:47 am

Possibly Related Threads...
Thread Author Replies Views Last Post
  Cryptocurrency in simple details. WinterHold 49 7504 September 10, 2021 at 11:02 am
Last Post: Spongebob
  Programming Language Swift Poll Shining_Finger 24 5854 December 2, 2015 at 7:21 pm
Last Post: bennyboy
  Programming Question Shining_Finger 8 1859 December 2, 2015 at 5:30 pm
Last Post: Tiberius
  Programming the Human Mind: Shining_Finger 21 6252 November 24, 2015 at 7:56 pm
Last Post: bennyboy
  Anyone into Android programming? emjay 97 25137 September 20, 2015 at 6:50 am
Last Post: bennyboy
  Advice Sought for Web Programming AFTT47 13 4241 April 4, 2015 at 10:41 pm
Last Post: bennyboy



Users browsing this thread: 1 Guest(s)