possible combination of a string

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2006
Posts: 1
Reputation: vijayr_singh is an unknown quantity at this point 
Solved Threads: 0
vijayr_singh vijayr_singh is offline Offline
Newbie Poster

possible combination of a string

 
0
  #1
Dec 13th, 2006
Hi friends...

I need to write a program in C which will print all the combinations of a string with non-repeating characters. Example: “Say” will have the following: S, a,y, Sa, Sy, aS,Sy, yS, ya, aSy,Sya,ySa and so on. The string length is not known. The string will be a command line argument to the program.


The code that i have goes below but its not giving me the desired output. the output that i m getting from this program is like

#############OUTPUT##################
Enter the string : say

sya
ysa
yas
ays
asy
say
##################################

#############DESIRED OUTPUT##################
Enter the string : say

s
a
y
sa
sy
as
ys
ay
ya
sya
ysa
yas
ays
asy
say
###########################################


###########PROGRAM################
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<alloc.h>
  4. #include<conio.h>
  5.  
  6.  
  7. void swap(char*,int);
  8. void gotoloop(char*,int);
  9.  
  10. void main()
  11. {
  12. char *ch;
  13. int i,j,k,l;
  14. ch=(char*)malloc(20);
  15. //clrscr();
  16. printf("Enter the string\n");
  17. gets(ch);
  18.  
  19. l=strlen(ch);
  20. gotoloop(ch,l);
  21.  
  22. return;
  23. }
  24.  
  25. void gotoloop(char *ch,int l)
  26. {
  27. int i,k;
  28. k=l;
  29.  
  30. if(l<=1)
  31. return;
  32.  
  33.  
  34. for(i=0;i<k;i++)
  35. {
  36. swap(ch,k);
  37. l--;
  38. gotoloop(ch,l);
  39. l++;
  40. if(k==2)
  41. printf("\n%s ",ch);
  42. }
  43.  
  44. }
  45.  
  46.  
  47. void swap(char *ch,int r)
  48.  
  49. {
  50. char c;
  51. int i;
  52.  
  53. c=ch[r-1];
  54. for(i=r-1;i>0;i--)
  55. ch[i]=ch[i-1];
  56. ch[0]=c;
  57. }


This is not like a normal permutation of string.

Can anyone help me on this ASAP.

Thanks in advance
Last edited by ~s.o.s~; Dec 13th, 2006 at 11:44 am. Reason: Added code tags learn to use them yourself.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: possible combination of a string

 
0
  #2
Dec 13th, 2006
Wow - a full house of forums I've seen the same post on.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,057
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 130
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: possible combination of a string

 
0
  #3
Dec 13th, 2006
Huh? Whatcha mean?
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: possible combination of a string

 
0
  #4
Dec 13th, 2006
>Huh? Whatcha mean?
He means that this question has been spammed on multiple website forums, such as Daniweb, cprogramming, etc...
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: possible combination of a string

 
0
  #5
Dec 13th, 2006
Originally Posted by Narue View Post
>Huh? Whatcha mean?
He means that this question has been spammed on multiple website forums, such as Daniweb, cprogramming, etc...
It could also be interpretated that Salem has so much free time on his hands he can afford to join every c forum in existence. Although he does spend a lot of time on times.co.uk website. How he can juggle the two I don't know. He he.
Last edited by iamthwee; Dec 13th, 2006 at 6:04 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: possible combination of a string

 
0
  #6
Dec 13th, 2006
>It could also be interpretated that Salem has so much free time on his
>hands he can afford to join every c forum in existence.
Please. Everyone knows that Salem is a bot.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 13
Reputation: dileepkumar235 has a little shameless behaviour in the past 
Solved Threads: 0
dileepkumar235 dileepkumar235 is offline Offline
Newbie Poster

Code for all possible combinations of a string using GOTO

 
-1
  #7
Jan 18th, 2009
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char a[20],st_char;
static int i,j,k,n,st,ctr,main_ctr;

printf("Enter the string : ");
gets(a);

n=strlen(a);

if(n<=1)
{
printf("please enter a valid string ");
exit(0);
}

label :

for(i=0;i<=n-2;++i)
{
ctr=0;
printf("\n");
printf("%c",a[0]);
for(j=i+1;j<=n-1;j++)
{
printf("%c",a[j]);
ctr++;
}

if(ctr!=n-1)
//while(ctr!=n-1)
{
st=i+1;
for(k=1;k<=st-1;k++)
{
printf("%c",a[k]);
ctr++;
}
}
}

st_char=a[0];
for(i=0;i<=n-2;i++)
a[i]=a[i+1];

a[n-1]=st_char;

main_ctr++;

while(main_ctr<n)
goto label;

printf("Designed by Uday kumar and Dileep Basam ");
getch();
}
Last edited by dileepkumar235; Jan 18th, 2009 at 12:08 pm. Reason: hey sorry guys this will give just permutations of the string
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: possible combination of a string

 
0
  #8
Jan 18th, 2009
Please use code tags!

Amen to Salem being a bot ;P

You posted it everywhere...pretty lame lol. What amazes me is how you managed to come up with code for permutations, but cannot write what appears to be a simple loop....

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: possible combination of a string

 
0
  #9
Jan 18th, 2009
You missed to read about the forum posting expectations and proper code tagging.
I suggest you stick around and learn why your posted code is not a good example.
Last edited by Aia; Jan 18th, 2009 at 12:30 pm.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: possible combination of a string

 
0
  #10
Jan 18th, 2009
Not to mention the unmentionable, using gets().
A thoroughly deserved 0/10.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 12710 | Replies: 13
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC