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
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 |
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 |
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 |
/*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 |
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 |
#define SQR(x) x * x main() { printf("%d", 225/SQR(15)); } a. 1 b. 225 c. 15 d. none of the above
#define assert(cond) if(!(cond)) \ (fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\ __FILE__,__LINE__), abort()) void main() { int i = 10; if(i==0) assert(i < 100); else printf("This statement becomes else for if in assert macro"); }
What is the problem with the following code segment? while ((fgets(receiving array,50,file_ptr)) != EOF) ;
main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); }
void main() { int i=i++,j=j++,k=k++; printf(“%d%d%d”,i,j,k); }
Is it possible to print a name without using commas, double quotes,semi-colons?
main() { int x=5; clrscr(); for(;x==0;x--) { printf("x=%d\n”", x--); } } a. 4, 3, 2, 1, 0 b. 1, 2, 3, 4, 5 c. 0, 1, 2, 3, 4 d. none of the above
Question: We would like to design and implement a programming solution to the reader-writer problem using semaphores in C language under UNIX. We assume that we have three readers and two writers processes that would run concurrently. A writer is to update (write) into one memory location (let’s say a variable of type integer named temp initialized to 0). In the other hand, a reader is to read the content of temp and display its content on the screen in a formatted output. One writer can access the shared data exclusively without the presence of other writer or any reader, whereas, a reader may access the shared memory for reading with the presence of other readers (but not writers).
Finding a number which was log of base 2
int a=1; printf("%d %d %d",a++,a++,a); need o/p in 'c' and what explanation too
write a program to find out roots of quadratic equation "x=-b+-(b^2-4ac0^-1/2/2a"
3) Int Matrix of certain size was given, We had few valu= es in it like this. =97=97=97=97=97=97=97=97=97=97=97 1 = | 4 | | 5 | &= nbsp; | 45 =97=97=97=97=97=97=97=97=97=97=97 &n= bsp; | 3 | 3 | 5 | = | 4 =97=97=97=97=97=97=97=97=97=97=97 34 |&nbs= p; 3 | 3 | | 12 | &= nbsp; =97=97=97=97=97=97=97=97=97=97=97 3 | &nbs= p; | 3 | 4 | = | 3 =97=97=97=97=97=97=97=97=97=97=97 3 | = ; | | | = ; 3 | =97=97=97=97=97=97=97=97=97=97=97 &= nbsp; | | 4 | = ; | 4 | 3 We w= ere supposed to move back all the spaces in it at the end. Note: = If implemented this prog using recursion, would get higher preference.