Submitted Questions

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

    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.