944,148 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 18734
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 13th, 2006
-1

possible combination of a string

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vijayr_singh is offline Offline
1 posts
since Dec 2006
Dec 13th, 2006
0

Re: possible combination of a string

Wow - a full house of forums I've seen the same post on.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 13th, 2006
0

Re: possible combination of a string

Huh? Whatcha mean?
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 163
The Queen of DaniWeb
cscgal is offline Offline
13,646 posts
since Feb 2002
Dec 13th, 2006
0

Re: possible combination of a string

>Huh? Whatcha mean?
He means that this question has been spammed on multiple website forums, such as Daniweb, cprogramming, etc...
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 13th, 2006
0

Re: possible combination of a string

Click to Expand / Collapse  Quote originally posted by Narue ...
>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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Dec 13th, 2006
0

Re: possible combination of a string

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 18th, 2009
-1

Code for all possible combinations of a string using GOTO

#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
Reputation Points: 5
Solved Threads: 0
Newbie Poster
dileepkumar235 is offline Offline
13 posts
since Jan 2009
Jan 18th, 2009
0

Re: possible combination of a string

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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Jan 18th, 2009
0

Re: possible combination of a string

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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 18th, 2009
0

Re: possible combination of a string

Not to mention the unmentionable, using gets().
A thoroughly deserved 0/10.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: weird sigabort from fclose
Next Thread in C Forum Timeline: Help.. Turbo c program..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC