|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Need help with getting all combinations of a string
Hello
,I have a problem. I have been given a task (in the form of homework) to create a program that finds all possible combinations of a string, whatever this string may be. I'we been trying to figure this out for 2-3 days now but I have been able to solve it. This is the task: Create a program where the user can enter a string. Then the program will find and print all possible combinations of the chars in the string. I'we made 3 or 4 atempts at this but have any been able to acomplish Slow, Clumsy and generaly bad code that doesnt even work. Can someone please help me? I've been searching google forever on it to
__________________
Real Programmers use C since it's the easiest language to spell |
|
#2
|
||||
|
||||
|
Here are two examples of my atempts:
Code:
program Uppg3;
{$APPTYPE CONSOLE}
uses Crt32;
var numl, temp, combin, num : integer;
var Combinations : array[0..100000] of string;
var current, original : string;
function CombinationExists : integer;
begin
temp := -1;
While(temp < combin) Do
begin
temp := temp + 1;
if(Combinations[temp] = current) then
begin
CombinationExists := 1;
temp := combin;
end
else
begin
CombinationExists := 2;
end;
end;
end;
begin
temp := -1;
While(temp < 100000) Do
begin
temp := temp + 1;
Combinations[temp] := '';
end;
temp := -1;
Write('Please enter original string here: ');
ReadLn(original);
numl := Length(original);
combin := numl * numl;
While(num < combin) Do
begin
While(temp < numl) Do
begin
temp := temp + 1;
current[temp] := original[Random(numl)];
end;
if(CombinationExists = 0) then
begin
num := num + 1;
Combinations[num] := current;
WriteLn(Combinations[num]);
end;
temp := -1;
end;
ReadLn;
end.
Code:
program Uppg3;
{$APPTYPE CONSOLE}
uses Crt32, Dialogs;
var matrix : array[0..1000000] of string;
var init : string;
var temp : string[255];
var max, numl, numc, num, nums, tempn : integer;
begin
numc := -1;
num := -1;
tempn := 0;
Write('Please enter original string here: ');
ReadLn(init);
numl := Length(init);
max := numl * numl;
ClrScr;
While(num < max) Do
begin
numc := numc + 1;
num := num + 1;
While(tempn < numl) Do
begin
tempn := tempn + 1;
if(tempn <> numl) then
begin
if(tempn = numc) then
begin
temp[tempn] := init[tempn];
end
else if(tempn = numc + 1) then
begin
temp[tempn] := init[tempn - 2];
end
else
begin
temp[tempn] := init[tempn - 1];
end;
end
else
begin
temp[0] := init[numl];
temp[numl] := init[1];
end;
end;
matrix[num] := temp;
tempn := -1;
end;
num := -1;
While(num < max) Do
begin
num := num + 1;
WriteLn(matrix[num]);
end;
ReadLn;
end.
the secunde one does acctually do... something alteast |
|
#3
|
|||
|
|||
|
at this url : http://www.delphiforfun.org/Programs/Permutes_2.htm there is some source code that does permutations and combinations just download the project source code. You can look through the source code and run it to see if it can help you to understand how to solve your problem
|
|
#4
|
||||
|
||||
|
thanks ^^ I'll have a look at it
![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Delphi Programming > Need help with getting all combinations of a string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|