Write out a function that prints out all the permutations of
a string.

For example, abc would give you abc, acb, bac, bca, cab,
cba. You can assume that all the characters will be unique.

Answers were Sorted based on User's Feedback



Write out a function that prints out all the permutations of a string. For example, abc would g..

Answer / fallen angel

use plain recursion
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void swap(char* src, char* dst)
{
char ch = *dst;
*dst = *src;
*src = ch;
}
/* permute [set[begin], set[end]) */
int permute(char* set, int begin, int end)
{
int i;
int range = end - begin;
if (range == 1) {
printf("set: %s\n", set);
} else {
for(i=0; i<range; i++) {
swap(&set[begin], &set[begin+i]);
permute(set, begin+1, end);
swap(&set[begin], &set[begin+i]);
/* set back */
}
}
return 0;
}
int main()
{
char str[] = "abcd";
permute(str, 0, strlen(str));
return 0;
}

Is This Answer Correct ?    28 Yes 9 No

Write out a function that prints out all the permutations of a string. For example, abc would g..

Answer / patrick

//I think it can be implemented this way rather
simply..check it..

main(){
char a[15];
int len,fiblen,i,j,count=0,div,on,to;
printf("enter the string\n");
scanf("%s",a);
fiblen=fib(strlen(a));
div=fiblen/(strlen(a));
for(i=0;i<strlen(a);i++){
for(j=0;j<(strlen(a)-1);j++){
for(on=1;on<(strlen(a)-1);on++){
to=on+1;
swap(&a[on],&a[to]);
count++;//no: of anagram
printf("(%d) %s\n",count,a);
}
}
swap(&a[0],&a[i+1]);
}
}
swap(char *a,char *b){
char temp=*a;
*a=*b;
*b=temp;
}
int fib(int a){
if(a==1)
return(1);
else
return(a*fib(a-1));
}

Is This Answer Correct ?    4 Yes 2 No

Write out a function that prints out all the permutations of a string. For example, abc would g..

Answer / raghuram.a

/*guys..I have implemented Jhonson trotter algorithm..u can
print permutations of 123..n. implement the same for strings!*/


#include<iostream.h>
#include<conio.h>
int min(int a[10],int n)
{
int i,m=1;
for(i=2;i<=n;i++)
{
if(a[m]>a[i])
m=i;
}
return m;
}

void swap(int &a,int &b)
{
int t;
t=a;
a=b;
b=t;
}
int main()
{
int i,j,k,n,flag=0,l,m;
int d[100],a[100];
clrscr();
cout<<"\n\nenter n:\n\n";
cin>>n;
for(i=1;i<=n;i++)
a[i]=i;
for(i=1;i<=n;i++)
d[i]=i-1;
cout<<"\n\npermutations generated for integer 12...n
are:\n\n";
for(i=1;i<=n;i++) //display the given no.
cout<<a[i];
cout<<"\t";
do
{
flag=0;
k=min(a,n);
for(i=1;i<=n;i++)
{
if((a[i]>a[k])&&(d[i]!=0)&&(a[d[i]]<a[i])) //find
mobile integer.
k=i;
}
if(a[k]==1)
break;
l=d[k]; //copy of direction of mobile integer.
m=a[k]; //copy of mobile integer.


if(d[k]==k+1&&d[k+1]==k) //swap directions.
{

if(d[k]==n)
{ d[k+1]=0;
d[k]=k-1;
}

else if(d[k+1]==1)
{
d[k]=0;
d[k+1]=k+2;
}
else
{ d[k]=k-1;
d[k+1]=k+2;
}
}
else if(d[k]==k-1&&d[k-1]==k)
{
d[k]=k+1;
d[k-1]=k-2;
}
/*cout<<d[1]<<d[2]<<d[3]<<d[4];
cout<<"\t";
if u want to know directions of integers.
*/
swap(a[k],a[l]); //swap mobile integer and integer it is
pointing to.
for(i=1;i<=n;i++)
cout<<a[i]; //display the number.
cout<<"\t";
for(i=1;i<=n;i++) //reverse directions of integers
greater than
//mobile integer.
{
if(a[i]>m)
{
if(d[i]==0&&i==n)
d[i]=i-1;
else if(d[i]<i)
d[i]=i+1;
else if(d[i]>i)
d[i]=i-1;
}
}
for(i=1;i<n;i++) //check whether a mobile integer exist or not.
{
if(a[i]<a[i+1])
{

if(d[i+1]!=0)
flag=1;
}

else if(a[i]>a[i+1])
{

if(d[i]!=0)
flag=1;
}
}
}
while(flag==1); //if no mobile
integer(flag=0)terminate the program.
getch();
return 0;
}

Is This Answer Correct ?    4 Yes 3 No

Write out a function that prints out all the permutations of a string. For example, abc would g..

Answer / raghuram

/*guys..I have implemented Jhonson trotter algorithm..u can
print permutations of 123..n. implement the same for strings!*/


#include<iostream.h>
#include<conio.h>
int min(int a[10],int n)
{
int i,m=1;
for(i=2;i<=n;i++)
{
if(a[m]>a[i])
m=i;
}
return m;
}

void swap(int &a,int &b)
{
int t;
t=a;
a=b;
b=t;
}
int main()
{
int i,j,k,n,flag=0,l,m;
int d[100],a[100];
clrscr();
cout<<"\n\nenter n:\n\n";
cin>>n;
for(i=1;i<=n;i++)
a[i]=i;
for(i=1;i<=n;i++)
d[i]=i-1;
cout<<"\n\npermutations generated for integer 12...n
are:\n\n";
for(i=1;i<=n;i++) //display the given no.
cout<<a[i];
cout<<"\t";
do
{
flag=0;
k=min(a,n);
for(i=1;i<=n;i++)
{
if((a[i]>a[k])&&(d[i]!=0)&&(a[d[i]]<a[i])) //find
mobile integer.
k=i;
}
if(a[k]==1)
break;
l=d[k]; //copy of direction of mobile integer.
m=a[k]; //copy of mobile integer.


if(d[k]==k+1&&d[k+1]==k) //swap directions.
{

if(d[k]==n)
{ d[k+1]=0;
d[k]=k-1;
}

else if(d[k+1]==1)
{
d[k]=0;
d[k+1]=k+2;
}
else
{ d[k]=k-1;
d[k+1]=k+2;
}
}
else if(d[k]==k-1&&d[k-1]==k)
{
d[k]=k+1;
d[k-1]=k-2;
}
/*cout<<d[1]<<d[2]<<d[3]<<d[4];
cout<<"\t";
if u want to know directions of integers.
*/
swap(a[k],a[l]); //swap mobile integer and integer it is
pointing to.
for(i=1;i<=n;i++)
cout<<a[i]; //display the number.
cout<<"\t";
for(i=1;i<=n;i++) //reverse directions of integers
greater than
//mobile integer.
{
if(a[i]>m)
{
if(d[i]==0&&i==n)
d[i]=i-1;
else if(d[i]<i)
d[i]=i+1;
else if(d[i]>i)
d[i]=i-1;
}
}
for(i=1;i<n;i++) //check whether a mobile integer exist or not.
{
if(a[i]<a[i+1])
{

if(d[i+1]!=0)
flag=1;
}

else if(a[i]>a[i+1])
{

if(d[i]!=0)
flag=1;
}
}
}
while(flag==1); //if no mobile
integer(flag=0)terminate the program.
getch();
return 0;
}

Is This Answer Correct ?    5 Yes 5 No

Write out a function that prints out all the permutations of a string. For example, abc would g..

Answer / shikha

#include <iostream>

using namespace std;

void anagram(char x[],string temp){
int size = 0;
while(x[size] != '\0'){
size++;
}

// cout << size << " " << x << endl;
if(size > 0){
for(int i=0; i<size ; i++){
string temp1 = temp + x[i];
char y[size];
for(int j=0,k=0; j<size-1 ; j++,k++){
if(k != i) y[j] = x[k];
else j--;
}
y[size-1] = '\0';
anagram(y,temp1);
}
}
else{
cout << temp << endl;
temp = "";
}
}

int main(){
char name[] = "abc";
anagram(name, "");
return 0;
}

Is This Answer Correct ?    1 Yes 2 No

Post New Answer

More C Code Interview Questions

main() { float f=5,g=10; enum{i=10,j=20,k=50}; printf("%d\n",++k); printf("%f\n",f<<2); printf("%lf\n",f%g); printf("%lf\n",fmod(f,g)); }

1 Answers  


main() { signed int bit=512, i=5; for(;i;i--) { printf("%d\n", bit >> (i - (i -1))); } } a. 512, 256, 0, 0, 0 b. 256, 256, 0, 0, 0 c. 512, 512, 512, 512, 512 d. 256, 256, 256, 256, 256

2 Answers   HCL,


Write a single line c expression to delete a,b,c from aabbcc

2 Answers   Microsoft,


How will you print % character? a. printf(“\%”) b. printf(“\\%”) c. printf(“%%”) d. printf(“\%%”)

4 Answers   HCL,


main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; for (i=0;i<=4;i++) printf("%s",names[i]); }

2 Answers  






4. Main() { Int i=3,j=2,c=0,m; m=i&&j||c&I; printf(“%d%d%d%d”,I,j,c,m); }

2 Answers   Broadridge,


int i; main(){ int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2,3 find the o/p

2 Answers  


can you use proc sql to manpulate a data set or would u prefer to use proc report ? if so why ? make up an example and explain in detail

0 Answers   TCS,


How to palindrom string in c language?

6 Answers   Google,


find A^B using Recursive function

2 Answers  


write a origram swaoing valu without 3rd variable

2 Answers  


main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); }

7 Answers  


Categories