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                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
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() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); }  1
main() { int i=5; printf("%d",++i++); }  1
main() { int *j; { int i=10; j=&i; } printf("%d",*j); }  1
main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); }  1
main() { int i = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }  1
#include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); }  1
to remove the repeated cahracter from the given caracter array. i.e.., if the input is SSAD output should of SAD Synergy6
main() { char a[4]="HELLO"; printf("%s",a); }  1
main() { char *p; p="Hello"; printf("%c\n",*&*p); }  1
To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates. Synergy2
main() { printf("%d", out); } int out=100;  1
typedef struct error{int warning, error, exception;}error; main() { error g1; g1.error =1; printf("%d",g1.error); }  1
main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }  1
Is the following code legal? struct a { int x; struct a b; }  1
void main() { int i=10, j=2; int *ip= &i, *jp = &j; int k = *ip/*jp; printf(“%d”,k); }  1
How will u find whether a linked list has a loop or not? Microsoft6
main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } }  1
main() { struct date; struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }  1
#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
print a semicolon using Cprogram without using a semicolon any where in the C code in ur program!! Tata-Elxsi18
 
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