Thread: Printing all combinations of a string loop problem

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    25

    Printing all combinations of a string loop problem

    Code:
    #include<stdio.h>
    #include<string.h>
    #define a 15
     
     int main()
    {
    	char str[a];
    	char *ptr=str;
    	char temp;
    	int i,n,j,b;
    printf("type a word to see all combinations:\n");
    	scanf("%s",ptr);
    	n=strlen(str);
    	for (j=1; j<=n; j++) {
    		for (i=0; i<n-1; i++) {
    		temp=str[i];
    		str[i]=str[i+1];
    		str[i+1]=temp;
    		printf("%s ",str);
    		}
    	}
    return0;
    	
    }
    I am trying to print all combinations of a string but I couldn't figure out how to do loop for it. Code works only 2-3-4 letters words

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Could you clarify what you mean by "all combinations of a string"? For example, if my string is "foo" then what would the different combinations be?

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    25
    Quote Originally Posted by c99tutorial View Post
    Could you clarify what you mean by "all combinations of a string"? For example, if my string is "foo" then what would the different combinations be?
    if ur string is foo there is 6 combinations of foo 3!=6 foo foo ofo ofo oof oof. I haven't thought yet about if there is/are repeating letters. I will work on it later if I can

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    I would start with the following approach:

    Code:
    char word[] = "foo";
    int len = strlen(word);
    int first, second, third;  // there should be len members
    
    for (first = 0; first < len; first++) {
      for (second = 0; second < len; second++) {
        if (second==first)
          continue;
        for (third = 0; third < len; third++) {
          if (third==first)
            continue;
          if (third==second)
            continue;
          printf("%c%c%c\n", word[first], word[second], word[third]);
        }
      }
    }
    You would obviously need to generalize it to be able to handle an arbitrary length string.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I think the best approach would to be to order the letters in the word and then go through all the lexicographic permutations.

    Permutation - Wikipedia, the free encyclopedia

    There is a lot of info out there for the algorithm, so I'll let you enjoy the researching.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 12-14-2010, 04:10 PM
  2. problem printing string
    By gkoenig in forum C Programming
    Replies: 4
    Last Post: 05-04-2008, 10:01 PM
  3. Having problem printing **string
    By Phoenix_Rebirth in forum C Programming
    Replies: 6
    Last Post: 01-06-2006, 05:10 PM
  4. printing a string problem
    By r0ss in forum C Programming
    Replies: 3
    Last Post: 11-06-2003, 02:49 PM
  5. problem with printing a structed array using for loop
    By Prezo in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2002, 09:00 AM