• C Program to Check Anagram

    Have you ever come across the term “anagram”? It’s a fascinating concept that involves rearranging the letters of a word or phrase to create a new word or phrase. Anagrams are not only fun to play with but also have practical applications in various fields like cryptography, word games, and even computer programming.

    In this blog post, we will explore the world of anagrams and learn how to write a C program to check if two strings are anagrams of each other. So, let’s dive in and unravel the mysteries of anagramming!

    Introduction to Anagrams

    Anagrams, in simple terms, are words or phrases that can be formed by rearranging the letters of another word or phrase. For example, the word “listen” can be rearranged to form “silent.” Similarly, the phrase “rail safety” can be rearranged to form “fairy tales.”

    Anagrams are not limited to just words and phrases; they can also be applied to numbers or any other set of characters. The key concept here is rearranging the characters while keeping the same set intact.


    Anagram Checking Algorithm

    Now that we have a basic understanding of anagrams, let’s move on to the main topic: checking if two strings are anagrams of each other using a C program. To achieve this, we will follow a simple algorithm:

    1. Take two input strings from the user.

    2. Compare the lengths of both strings. If they are not equal, we can conclude that the strings are not anagrams.

    3. Convert both strings to lowercase to simplify the comparison process. This step ensures that the comparison is case-insensitive.

    4. Sort both strings alphabetically.

    5. Compare the sorted strings character by character. If at any point, the characters don’t match, the strings are not anagrams.

    6. If all characters match till the end, we can determine that the strings are anagrams of each other.

    Now that we have a clear algorithm in mind, let’s start writing the C program!


    Writing the C Program

    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    void convertToLowercase(char *str) {
        for (int i = 0; i < strlen(str); i++) {
            str[i] = tolower(str[i]);
        }
    }
    
    void sortString(char *str) {
        int n = strlen(str);
        for (int i = 0; i < n-1; i++) {
            for (int j = i+1; j < n; j++) {
                if (str[i] > str[j]) {
                    char temp = str[i];
                    str[i] = str[j];
                    str[j] = temp;
                }
            }
        }
    }
    
    int checkAnagram(char *str1, char *str2) {
        if (strlen(str1) != strlen(str2)) {
            return 0; // Not anagrams if lengths are different
        }
    
        convertToLowercase(str1);
        convertToLowercase(str2);
    
        sortString(str1);
        sortString(str2);
    
        if (strcmp(str1, str2) == 0) {
            return 1; // Anagrams if sorted strings are equal
        } else {
            return 0; // Not anagrams
        }
    }
    
    int main() {
        char str1[100], str2[100];
    
        printf("Enter the first string: ");
        gets(str1);
    
        printf("Enter the second string: ");
        gets(str2);
    
        if (checkAnagram(str1, str2)) {
            printf("The strings are anagrams of each other.\n");
        } else {
            printf("The strings are not anagrams.\n");
        }
    
        return 0;
    }

    Explaining the C Program

    Now, let’s go through the program step by step to understand how it checks for anagrams.

    The program starts by including three header files: stdio.h for basic I/O operations, ctype.h for converting characters to lowercase, and string.h for string manipulation functions.

    We define three helper functions:

    1. convertToLowercase: This function takes a string as input and converts all characters to lowercase using the tolower function from ctype.h.

    2. sortString: This function takes a string as input and sorts it alphabetically using the Bubble Sort algorithm.

    3. checkAnagram: This function takes two strings as input, checks if their lengths are equal, converts them to lowercase, sorts them, and compares them using the strcmp function from string.h.

    In the main function, we declare two character arrays str1 and str2 to store the user input strings.

    We prompt the user to enter the first string using printf and read the input using gets function. Similarly, we do the same for the second string.

    We then call the checkAnagram function with the two input strings. If the function returns 1, it means the strings are anagrams, and we display the corresponding message. Otherwise, we display the message indicating that the strings are not anagrams.


    Testing the Program

    Now that we have written the C program, let’s test it with different inputs to see if it correctly detects anagrams.

    Example-1:

    Enter the first string: listen
    Enter the second string: silent
    The strings are anagrams of each other.

    Example-2:

    Enter the first string: hello
    Enter the second string: olleh
    The strings are anagrams of each other.

    Example-3:

    Enter the first string: cat
    Enter the second string: dog
    The strings are not anagrams.

    As you can see, the program successfully detects anagrams and non-anagrams.


    Conclusion

    Congratulations!  Overall, You’ve successfully written a C program to check whether two strings are anagrams. We covered the fundamental concepts of anagrams, outlined a plan of attack, and implemented the code step by step. You should now have a solid understanding of how to approach problems involving string manipulation in C.

    Remember, anagrams are just one exciting aspect of programming. Keep exploring and challenging yourself with new concepts and problems to become a better developer. Happy coding!

    Further Steps:

    If you want to delve deeper into the topic and enhance your understanding of string manipulation in C, here are a few suggested steps:

    1. Learn about other string functions available in the C programming language, such as strcpy, strcat, strchr, and strstr.

    2. Experiment with different variations of anagrams, such as case-insensitive anagrams (ignore letter case) or anagrams of phrases (including spaces).

    3. Expand the program to handle multiple strings and check if they all are anagrams of each other.

    4. Explore more advanced algorithms and data structures for efficient string manipulation, such as tries and suffix trees.

    5. Practice solving coding challenges and puzzles related to anagrams on coding platforms like LeetCode or HackerRank.

    Remember, the journey of learning never stops. Keep pushing your boundaries and enjoy the infinite possibilities that the world of programming has to offer. Happy coding!