Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...

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..

//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..

/*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..

/*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..

#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

More C Code Interview Questions

const int perplexed = 2; #define perplexed 3 main() { #ifdef perplexed #undef perplexed #define perplexed 4 #endif printf("%d",perplexed); } a. 0 b. 2 c. 4 d. none of the above

write a program for area of circumference of shapes

how can i cast a char type array to an int type array

# include <stdio.h> int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); }

write a c program to Create a registration form application by taking the details like username, address, phone number, email along with password and confirm password (should be same as password).Ensure that the password is of 8 characters with only numbers and alphabets. Take such details for 5 users and display the details. In place of password display “****”. (Use Structures).

what will be the output of this program? void main() { int a[]={5,10,15}; int i=0,num; num=a[++i] + ++i +(++i); printf("%d",num); }

main(){ char a[100]; a[0]='a';a[1]]='b';a[2]='c';a[4]='d'; abc(a); } abc(char a[]){ a++; printf("%c",*a); a++; printf("%c",*a); }

main() { static int a[3][3]={1,2,3,4,5,6,7,8,9}; int i,j; static *p[]={a,a+1,a+2}; for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j), *(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i)); } }

main() { int i, j; scanf("%d %d"+scanf("%d %d", &i, &j)); printf("%d %d", i, j); } a. Runtime error. b. 0, 0 c. Compile error d. the first two values entered by the user

void main() { int const * p=5; printf("%d",++(*p)); }

#define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); }

x=2 y=3 z=2 x++ + y++; printf("%d%d" x,y);

Categories