RE: Here's a simple programming problem for you to solve
April 28, 2016 at 7:47 am
(This post was last modified: April 28, 2016 at 8:02 am by bennyboy.)
My contribution. I can't be bothered with pure c either, but I wanted to show pool some recursion:
http://cpp.sh/5lfu
PS It's the one time I'm proud to say: SteelCurtain, mine's smaller than yours!
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!
