Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 4th, 2005, 02:52 PM
Kimm's Avatar
Kimm Kimm is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Sweden
Posts: 152 Kimm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 31 m 11 sec
Reputation Power: 5
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

Reply With Quote
  #2  
Old February 4th, 2005, 05:09 PM
Kimm's Avatar
Kimm Kimm is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Sweden
Posts: 152 Kimm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 31 m 11 sec
Reputation Power: 5
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

Reply With Quote
  #3  
Old February 4th, 2005, 06:03 PM
room24 room24 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 28 room24 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 27 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old February 5th, 2005, 07:53 AM
Kimm's Avatar
Kimm Kimm is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Sweden
Posts: 152 Kimm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 31 m 11 sec
Reputation Power: 5
thanks ^^ I'll have a look at it

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > Need help with getting all combinations of a string


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT