| | |
possible combination of a string
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2006
Posts: 1
Reputation:
Solved Threads: 0
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################
This is not like a normal permutation of string.
Can anyone help me on this ASAP.
Thanks in advance
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################
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<string.h> #include<alloc.h> #include<conio.h> void swap(char*,int); void gotoloop(char*,int); void main() { char *ch; int i,j,k,l; ch=(char*)malloc(20); //clrscr(); printf("Enter the string\n"); gets(ch); l=strlen(ch); gotoloop(ch,l); return; } void gotoloop(char *ch,int l) { int i,k; k=l; if(l<=1) return; for(i=0;i<k;i++) { swap(ch,k); l--; gotoloop(ch,l); l++; if(k==2) printf("\n%s ",ch); } } void swap(char *ch,int r) { char c; int i; c=ch[r-1]; for(i=r-1;i>0;i--) ch[i]=ch[i-1]; ch[0]=c; }
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.
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

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
•
•
•
•
>Huh? Whatcha mean?
He means that this question has been spammed on multiple website forums, such as Daniweb, cprogramming, etc...
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*
•
•
Join Date: Jan 2009
Posts: 13
Reputation:
Solved Threads: 0
#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();
}
#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
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.
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
![]() |
Similar Threads
- write a program which take a string give it permutations and combination (C++)
- write a program which take a string give it permutations and combination (C++)
- take string give it all possible combination (C++)
- Conver int Array into a String (Java)
Other Threads in the C Forum
- Previous Thread: how to learn c
- Next Thread: How can I find the L.C.M of a series of numbers
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures suggestions system systemcall test testautomation unix user voidmain() wab win32api windows.h






