Thread: Anagram

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    16

    Post Anagram

    I found an anagram program.. but it was designed for unix. Can someone help me convert it to windows? Or if someone has a better anagram algorithm?

    Code:
    ============================= canonize.c ===================================
    #include <stdio.h>
    #include <string.h>
    
    int charcmp(const char *c1, const char *c2) {
      return(*c1-*c2);
    }
    
    int main(int argc, char **argv) {
    char word[256], canon[256];
      for (;;) {
        if (gets(word) == NULL) break;
        strcpy(canon, word);
        qsort(canon, strlen(canon), sizeof(char), charcmp);
        printf("%s=%s\n", canon, word);
      }
      return(0);
    }
    ============================== gather.c ====================================
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv) {
    char line[256], lastcanon[256], lastword[256], *word;
      strcpy(lastcanon, "*Undefined*");
      for (;;) {
        if (gets(line) == NULL) break;
        if ((word = strchr(line, '=')) == NULL) break;
        *word++ = '\0';
        if (strcmp(line, lastcanon) == 0) {
          printf("%s = %s\n", lastword, word);
        }
        strcpy(lastcanon, line);
        strcpy(lastword, word);
      }
      return(0);
    }
    Also, will it take a long time to find all possible words using only 7 specifc letters on a P4 using this? Probably not but just curious.

    Thanks
    Helix

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    i dont see why this wont work with a windows compiler?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Looks like ANSI C to me. Compile away. Or did you mean with a fancy window and all that extra baggage?

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Is that supposed to be the same program or two seperate programs? Because if that's the same program it's not going to work due to main being defined twice.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    I made two differnet files and linked them into a project in Visual C++ (Ms). It gives an error which says that the Main method is already defined.

    There are two main methods in each file and I can't find what one has to do with another.

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Quote Originally Posted by vex_helix
    I made two differnet files and linked them into a project in Visual C++ (Ms). It gives an error which says that the Main method is already defined.

    There are two main methods in each file and I can't find what one has to do with another.
    Make two separate projects

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    Suppose you have 7 letters, how does one find all possible words from those letters? I can be a two letter, 3 word?

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I know this is the C forum but let me just ask. If you are not oppossed to using some C++ STL functions/concepts, then there are some handy functions and containers that can make this a snap, i.e. the string and set containers and the next_permutation and sort functions in particular. Just a thought, no harm in asking is there? Only about 9 line of code.

    If not, one way to do this might be to have whatever function you have that jumbles the letters around also jumble around an extra space or NULL character in with the other letters. Then when it comes to printing, you print the whole string up to that space or NULL character. Thus you end up with output that may be one, two, three, etc. characters long based on where ever that space/NULL character got placed. Of course you may still end up with some strings output more than once. If for example the user wants to find all permutations of the word "back" then "ba<NULL>ck" and "ba<NULL>kc" would both be valid permutations of the original word but when output under this algorithm, you would see "ba" output twice. So, there are things to consider.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. anagram program
    By meriororen in forum C Programming
    Replies: 12
    Last Post: 07-06-2009, 12:52 PM
  2. anagram program help
    By pjr5043 in forum C++ Programming
    Replies: 50
    Last Post: 04-28-2008, 07:32 PM
  3. Issues with Anagram Program
    By toadkiwi in forum C Programming
    Replies: 2
    Last Post: 04-16-2008, 09:20 PM
  4. Anagram program
    By cdonlan in forum C++ Programming
    Replies: 4
    Last Post: 04-18-2005, 12:04 PM
  5. HELP!!! Big-O of my anagram program
    By Ham in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2001, 01:38 PM