ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip       Ask Questions on ANYTHING, that arise in your Daily Life at     FORUM9.COM
Google
 
Categories  >>  Code Snippets  >>  Programming Code  >>  C Code
 
 


 

 
 C Code interview questions  C Code Interview Questions
 C++ Code interview questions  C++ Code Interview Questions
 VC++ Code interview questions  VC++ Code Interview Questions
 Java Code interview questions  Java Code Interview Questions
 Dot Net Code interview questions  Dot Net Code Interview Questions
 Visual Basic Code interview questions  Visual Basic Code Interview Questions
 Programming Code AllOther interview questions  Programming Code AllOther Interview Questions
Question
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.
 Question Submitted By :: Coder_1
I also faced this Question!!     Rank Answer Posted By  
 
  Re: 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.
Answer
# 1
/*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 ?    1 Yes 0 No
Raghuram
 
  Re: 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.
Answer
# 2
/*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 ?    1 Yes 0 No
Raghuram.A
 
 
 
  Re: 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.
Answer
# 3
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 ?    11 Yes 5 No
Fallen Angel
 
  Re: 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.
Answer
# 4
//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 ?    1 Yes 0 No
Patrick
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
main() { int i=-1; -i; printf("i = %d, -i = %d \n",i,-i); }  1
main() { int i, j, *p; i = 25; j = 100; p = &i; // Address of i is assigned to pointer p printf("%f", i/(*p) ); // i is divided by pointer p } a. Runtime error. b. 1.00000 c. Compile error d. 0.00000 HCL1
What are the files which are automatically opened when a C file is executed?  1
# include<stdio.h> aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); }  1
main() { char str1[] = {‘s’,’o’,’m’,’e’}; char str2[] = {‘s’,’o’,’m’,’e’,’\0’}; while (strcmp(str1,str2)) printf(“Strings are not equal\n”); }  1
How will you print % character? a. printf(“\%”) b. printf(“\\%”) c. printf(“%%”) d. printf(“\%%”) HCL1
Write a function to find the depth of a binary tree. Adobe8
main() { int a=10,*j; void *k; j=k=&a; j++; k++; printf("\n %u %u ",j,k); }  1
main() { char *cptr,c; void *vptr,v; c=10; v=0; cptr=&c; vptr=&v; printf("%c%v",c,v); }  1
In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'. main(){ int * j; void fun(int **); fun(&j); } void fun(int **k) { int a =0; /* add a stmt here*/ }  1
main() { char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); }  1
#include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s=malloc(sizeof(struct xx)); printf("%d",s->x); printf("%s",s->name); }  1
Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it. Wipro2
#include<stdio.h> main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d----%d",*p,*q); }  1
struct Foo { char *pName; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); strcpy(obj->pName,"Your Name"); printf("%s", obj->pName); } a. Your Name b. compile error c. Name d. Runtime error HCL1
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 HCL1
void main() { int k=ret(sizeof(float)); printf("\n here value is %d",++k); } int ret(int ret) { ret += 2.5; return(ret); }  1
Give a one-line C expression to test whether a number is a power of 2. Microsoft8
Which version do you prefer of the following two, 1) printf(“%s”,str); // or the more curt one 2) printf(str);  1
main() { int i =0;j=0; if(i && j++) printf("%d..%d",i++,j); printf("%d..%d,i,j); }  1
 
For more C Code Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com