Display different combinations of some letters forming a word

W.a.p in C to input a word. find the total no. of letters forming the word. and using these letters display the total no. of possible combinations without repeating any letter.

for example:

if string entered is "sunil", the total no. of letters in the word is 5 i.e. s,u,n,i,l.
and then displays
sunil
sunli
suinl
suiln
sulin
sulni ............. like this

again if string entered is "dipi", the total no. of letters forming the word is 3 i.e. d,i,p and displays
dip
pid
idp
dpi
pdi
ipd

Questions by mailmeskb

Showing Answers 1 - 3 of 3 Answers

amorjinx

  • Nov 24th, 2008
 

//Not the best way to do it.. I make ruthless use of memory

//but still gave it a shot ;)

#include <stdio.h>

#include <stdlib.h>

int wap_all(char *ip, char *op, int len, int occupied);

int main(int argc, char **argv)

{

char *ip,*op,*temp;

int i,len;

if(argc !=2)

{

printf("Improper usage. use wap <input string>n");

getch();

exit(1);

}

ip = argv[1];

for(i=0; ip[i]!='';i++);

len=i;

op = (char*)calloc(len,sizeof(char));

temp = (char*)calloc(len,sizeof(char));

for(i=0; i<len;i++)

temp[i]=ip[i];

printf("n");

wap_all(temp,op,len,0);

return 0;

}

int wap_all(char *ip, char *op, int len, int occupied)

{

int i,j,k;

char *temp;

if(len==0)

{

for(i=0;i<occupied;i++)

printf("%c",op[i]);printf(

"n");return 0;

}

temp = (char*)calloc(len-1,sizeof(char));

for(i=0;i<len;i++)

{

for(j=0,k=0;j<len-1;k++)

{

if(k==i)continue;

temp[j]=ip[k];

j++;

}

op[occupied]=ip[i];

wap_all(temp,op,len-1,occupied+1);

}

free(temp);

}

ishan786

  • Feb 15th, 2009
 

#include<stdio.h>

#include<conio.h>

char a[10];

int i,j,k;

void one();

void two();

void three();

void main()

{

       printf("n enter a word ");

       gets(a);

       for(i=0;a[i]!='';i++);

       if(i==1)

       one();

       else if(i==2)

       two();

       else if(i==3)

       three();

       getch();

}


void one()

{

       for(i=0;a[i]!='';i++)

       printf("n%c",a[i]);

}


void two()

{

       one();

       for(i=0;a[i]!='';i++)

       for(j=0;a[j]!='';j++)

       {

             
if(a[i]==a[j])

             
continue;

             
printf("n%c%c",a[i],a[j]);

       }

}


void three()

{

       one();

       two();

       for(i=0;a[i]!='';i++)

       {

             
for(j=0;a[j]!='';j++)

              {


                    
if(i==j)

                    
continue;

                    
for(k=0;a[k]!='';k++)

                    
{

                           
if((i==k)||(j==k))

                           
continue;

                           
printf("n%c%c%c",a[i],a[j],a[k]);

                    
}

              }


       }

}


similarly you can make combination of more letters, this is the simpler way i


think may be there are more so thanks

  Was this answer useful?  Yes

kamal Bhatia

  • Feb 13th, 2015
 

Code
  1. #include<stdio.h>

  2. #include<string.h>

  3. #define a 15

  4. int n,*p,cnt=1,i,j;

  5. char word[50];

  6. int check();

  7. void display();

  8. main()

  9. {

  10.         printf("Enter a word");

  11.         scanf("%s",word);

  12.         n = strlen(word);

  13.         p=(int *)calloc(n,sizeof(int));

  14.         while(1)

  15.                 for(i=1;i<=n;i++)

  16.                 {

  17.                         j=n-1;

  18.                         while(1)

  19.                         {

  20.                                

  21.                                 if(p[j]<n-1)

  22.                                 {

  23.                                   p[j]++;

  24.                                   if(check())

  25.                                         display();

  26.                                   break;

  27.                                 }

  28.                                 p[j]=0;

  29.                                 j--;

  30.                                 if(j<0)

  31.                                 {

  32.                                   getch();

  33.                                   exit(0);

  34.                                 }

  35.                         }      

  36.                 }

  37.                 getch();

  38. }

  39. int check()

  40. {

  41.         for(i=0;i<n-1;i++)

  42.                 for(j=i+1;j<n;j++)

  43.                         if(p[i]==p[j])

  44.                                 return 0;

  45.         return 1;

  46. }

  47. void display()

  48. {

  49.         printf("%d ",cnt++);

  50.         for(i=0;i<n;i++)

  51.                 printf("%c",word[p[i]]);

  52.         printf("

  53. ");

  54.         if(cnt%10==0)getch();

  55. }

  56.  



  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions