Thread: anagram strings

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    22

    anagram strings

    Would someone help me please, it has to check if the second word is anagram to the first one.
    So far ...

    Code:
    #include <stdio.h>
    
    void main()
    {
    
    
    char first[20], second[20];
    int x,y;
    int firstSum =0;
    int secondSum =0;
    
    printf ("enter a word: ");
    gets(first);
    
    printf ("enter an anagram: ");
    gets(second);
    
    while(first[x] !='\0')
    {   
        firstSum += first[x];
        x++;
    }
    
    while(second[y] !='\0')
    {
        secondSum += second[y];
        y++;
    }
    
    if (firstSum==secondSum)
    {
        printf(" is anagarm");
    }
    
    else
    {
    printf(" is not an anagarm");
    
    }
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    A couple things first:
    1. It's int main(void), and return an int at the end, usually zero for success. Read this: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com.
    2. Don't use gets. Read this: FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com.

    Now, to find an anagram, you don't just add up the numeric values of each character in the string. You have to count each letter independently. How many A's? How many B's? All the way down to Z.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    Thank for the tips.

    I'll think of counting each letter. I just want to know why this isn't working ?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by ph3s View Post
    Thank for the tips.

    I'll think of counting each letter. I just want to know why this isn't working ?
    You are using a sum of ascii numbers, it's not going to be unique eg.

    (1 + 2 + 3 + 4) = (1 + 1 + 3 + 5)

    You need to test each character from the source in the target, you can use strchr() to find a character in a string (or do it manually).


    Edit: this function returns 1 or 0 to signal true or false, it does not take string length of s1 into consideration however which is also necessary.

    Code:
    int is_anagram(char *s1, char *s2) {
        size_t len = strlen(s2);
        char *p = NULL;
    
        for(; *s2; s2++) {
            p = strchr(s1, *s2);
            if(p == NULL) {
                return 0;
            }
        }
        return 1;
    }
    Last edited by Subsonics; 03-08-2012 at 12:08 PM.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    My bad, I had to be more specific on my second question. The point is it crashes after I enter the second string into. I just want to know for now -why does it crashes?

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Hm, x and y are uninitialized which means you don't know if the index is out of range, you are entering a string that is more than 20 characters. Two possible reasons.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    x and y are uninitialized which means you don't know if the index is out of range
    exactly , thank you

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Don't think of counting each letter, do it. It's the only way*.

    Since first and second are just arrays, it makes sense to use a for loop, which makes it harder to forget initialization:
    Code:
    for (x = 0; first[x] != '\0'; x++) {
        increment count of letter in first[x]
    }
    As for subsonics implementation in post #4, it has a bug. It will produce false positives if, say, s2 has two 'q's and s1 only has 1.

    * Not really the only way, but it's probably the easiest and most intuitive way. There's also a method that involves sorting (case-insensitive) each string and comparing. Probably some other clever stuff too.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Yeah, that is true. What do you mean by counting letters? X keeps the count, are you referring to sum?

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I meant count the occurrences of each character.

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by anduril462 View Post
    I meant count the occurrences of each character.
    Ok I see. Here's an update using strchr btw.

    Code:
    int in_set(char *s1, char *s2) {
        for(char *p; *s2; s2++) {
            p = strchr(s1, *s2);
            if(p == NULL) {
                return 0;
            }
        }
        return 1;
    }
    
    int is_anagram(char *s1, char *s2) {
        if( strlen(s1) != strlen(s2) ) {
            return 0;
        }
    
        if(in_set(s1, s2)) {
            return in_set(s2, s1);
        }
    
        return 0;
    }

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    Thanks! Now I've got to learn what is strchr

  13. #13
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    @subsonics what youre saying is true using pointer arithmetic to check if the letters of both words are the same make the anagram

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    @Subsonics: Your code still doesn't work. Try "foo" and "off" as your two words. They are not anagrams. Your program reports them as false positives.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by ph3s View Post
    Thanks! Now I've got to learn what is strchr
    strchr man page

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. Anagram program
    By cdonlan in forum C++ Programming
    Replies: 4
    Last Post: 04-18-2005, 12:04 PM
  4. Anagram
    By vex_helix in forum C Programming
    Replies: 7
    Last Post: 04-05-2004, 12:18 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