Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 5th, 2006, 2:03 PM   #1
RemoteC2
Programmer
 
RemoteC2's Avatar
 
Join Date: Jan 2006
Posts: 55
Rep Power: 3 RemoteC2 is on a distinguished road
randStr

i just got done making a program thats goal is to take a string and generate all possible ways to re-arrange the letters in it. here is the source:

c Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <new>
  4. using namespace std;
  5.  
  6. void randStr(char *str);
  7.  
  8. int main()
  9. {
  10. char str[80];
  11. cout << endl << "enter string:";
  12. cin >> str;
  13. randStr(str);
  14. return 0;
  15. }
  16.  
  17. void randStr(char *str)
  18. {
  19. ofstream file;
  20. file.open("generatedStrings.txt");
  21. char *temp1, *temp2;
  22. int len = strlen(str);
  23. temp1 = new char [len];
  24. temp2 = new char [len];
  25. for(int i=0; i<len; i++) {
  26. temp1[i] = str[i];
  27. temp2[i] = temp1[i];
  28. }
  29. for(int a=0; a<len; a++) {
  30. for(int j=0; j<len; j++) {
  31. for(int k=1; k<len; k++) {
  32. str[k] = temp1[j];
  33. file << str << endl;
  34. for(int l=0; l<len; l++) {
  35. str[l] = temp2[l];
  36. }
  37. }
  38. }
  39. }
  40. file.close();
  41. }

and the output is this if i use 'anthony' for the input:

aathony
anahony
antaony
anthany
anthoay
anthona
anthony
annhony
antnony
anthnny
anthony
anthonn
atthony
anthony
anttony
anthtny
anthoty
anthont
ahthony
anhhony
anthony
anthhny
anthohy
anthonh
aothony
anohony
antoony
anthony
anthooy
anthono
anthony
annhony
antnony
anthnny
anthony
anthonn
aythony
anyhony
antyony
anthyny
anthoyy
anthony
aathony
anahony
antaony
anthany
anthoay
anthona
anthony
annhony
antnony
anthnny
anthony
anthonn
atthony
anthony
anttony
anthtny
anthoty
anthont
ahthony
anhhony
anthony
anthhny
anthohy
anthonh
aothony
anohony
antoony
anthony
anthooy
anthono
anthony
annhony
antnony
anthnny
anthony
anthonn
aythony
anyhony
antyony
anthyny
anthoyy
anthony
aathony
anahony
antaony
anthany
anthoay
anthona
anthony
annhony
antnony
anthnny
anthony
anthonn
atthony
anthony
anttony
anthtny
anthoty
anthont
ahthony
anhhony
anthony
anthhny
anthohy
anthonh
aothony
anohony
antoony
anthony
anthooy
anthono
anthony
annhony
antnony
anthnny
anthony
anthonn
aythony
anyhony
antyony
anthyny
anthoyy
anthony
aathony
anahony
antaony
anthany
anthoay
anthona
anthony
annhony
antnony
anthnny
anthony
anthonn
atthony
anthony
anttony
anthtny
anthoty
anthont
ahthony
anhhony
anthony
anthhny
anthohy
anthonh
aothony
anohony
antoony
anthony
anthooy
anthono
anthony
annhony
antnony
anthnny
anthony
anthonn
aythony
anyhony
antyony
anthyny
anthoyy
anthony
aathony
anahony
antaony
anthany
anthoay
anthona
anthony
annhony
antnony
anthnny
anthony
anthonn
atthony
anthony
anttony
anthtny
anthoty
anthont
ahthony
anhhony
anthony
anthhny
anthohy
anthonh
aothony
anohony
antoony
anthony
anthooy
anthono
anthony
annhony
antnony
anthnny
anthony
anthonn
aythony
anyhony
antyony
anthyny
anthoyy
anthony
aathony
anahony
antaony
anthany
anthoay
anthona
anthony
annhony
antnony
anthnny
anthony
anthonn
atthony
anthony
anttony
anthtny
anthoty
anthont
ahthony
anhhony
anthony
anthhny
anthohy
anthonh
aothony
anohony
antoony
anthony
anthooy
anthono
anthony
annhony
antnony
anthnny
anthony
anthonn
aythony
anyhony
antyony
anthyny
anthoyy
anthony
aathony
anahony
antaony
anthany
anthoay
anthona
anthony
annhony
antnony
anthnny
anthony
anthonn
atthony
anthony
anttony
anthtny
anthoty
anthont
ahthony
anhhony
anthony
anthhny
anthohy
anthonh
aothony
anohony
antoony
anthony
anthooy
anthono
anthony
annhony
antnony
anthnny
anthony
anthonn
aythony
anyhony
antyony
anthyny
anthoyy
anthony

and i was just wondering if this would be the correct output for the file? (meaning that all possibilities have been extinguished). if there is something wrong, please let me know.
RemoteC2 is offline   Reply With Quote
Old Nov 5th, 2006, 2:07 PM   #2
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>and i was just wondering if this would be the correct output for the file?
No. Look at the first and second strings:
aathony
anahony
Are either of those a permutation of "anthony"?
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Nov 5th, 2006, 2:08 PM   #3
RemoteC2
Programmer
 
RemoteC2's Avatar
 
Join Date: Jan 2006
Posts: 55
Rep Power: 3 RemoteC2 is on a distinguished road
hmmm..doesnt seem to be, but i think that i will look it up on google.
RemoteC2 is offline   Reply With Quote
Old Nov 5th, 2006, 5:50 PM   #4
Iftikhar
Programmer
 
Iftikhar's Avatar
 
Join Date: Oct 2006
Location: London
Posts: 40
Rep Power: 0 Iftikhar is on a distinguished road
Send a message via MSN to Iftikhar
It can not be correct according to mathematics also as word 'anthony' has 7 chracters in it. If you create a cross product of these chracters then chracter a can be in 7 positions like 'anthony', 'nathony', 'ntahony', and so on.
so there are n(n-1)! ways to arrange these letter in different ways where n is nummber of chracters in a string. so (7)(6)(5)(4)(3)(2) = 5040 possible combinations
__________________
Iftikhar Ahmed Khan
For doing an experiment on programmer's mood please visit http://uxisfyp1.brunel.ac.uk/cspgiak
Iftikhar is offline   Reply With Quote
Old Nov 5th, 2006, 10:54 PM   #5
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
Quote:
Originally Posted by Iftikhar View Post
Iso there are n(n-1)! ways to arrange these letter in different ways where n is nummber of chracters in a string.
n(n-1)! = n!
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:24 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC