Header-C
Header-C

 

Program Overview

This C program takes a list of strings and groups the anagrams together. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.

Program Structure

The program consists of the following key components:

  • Includes and Definitions: Necessary header files and constants are defined.
  • Sorting Function: A helper function that sorts the characters in each string to identify anagrams.
  • Main Function: The main function reads input, processes the strings, and prints grouped anagrams.
  • Data Structures: The program uses an array of strings to store the input and a map (using an array of linked lists) to store the groups of anagrams.

Code Implementation


#include <stdio.h>
#include <stdlib.h>
#include <string.h&gt>

#define MAX_STRINGS 100
#define MAX_LENGTH 100

// Function to compare two strings for sorting
int compare(const void *a, const void *b) {
    return strcmp(*(const char **)a, *(const char **)b);
}

// Function to sort a string
void sortString(char *str) {
    qsort(str, strlen(str), sizeof(char), compare);
}

// Function to group anagrams
void groupAnagrams(char *strs[], int strsSize) {
    // Array to hold sorted strings and their original counterparts
    char *sorted[MAX_STRINGS];
    char *anagrams[MAX_STRINGS][MAX_STRINGS];
    int count[MAX_STRINGS] = {0};

    for (int i = 0; i < strsSize; i++) {
        // Sort each string
        char *sortedStr = (char *)malloc(MAX_LENGTH * sizeof(char));
        strcpy(sortedStr, strs[i]);
        sortString(sortedStr);
        sorted[i] = sortedStr;

        // Grouping anagrams
        int found = 0;
        for (int j = 0; j <= i; j++) {
            if (j < i && strcmp(sorted[i], sorted[j]) == 0) {
                anagrams[j][count[j]] = strs[i];
                count[j]++;
                found = 1;
                break;
            }
        }
        if (!found) {
            anagrams[i][0] = strs[i];
            count[i] = 1;
        }
    }

    // Print grouped anagrams
    for (int i = 0; i < strsSize; i++) {
        if (count[i] > 0) {
            for (int j = 0; j < count[i]; j++) {
                printf("%s ", anagrams[i][j]);
            }
            printf("\n");
        }
    }

    // Free allocated memory
    for (int i = 0; i < strsSize; i++) {
        free(sorted[i]);
    }
}

int main() {
    char *strs[MAX_STRINGS] = {
        "eat", "tea", "tan", "ate", "nat", "bat"
    };
    int strsSize = 6;

    printf("Grouped Anagrams:\n");
    groupAnagrams(strs, strsSize);

    return 0;
}
        

Explanation of the Code

1. Headers: The program includes standard libraries for input/output, string manipulation, and memory allocation.

2. compare Function: This function is used by qsort to compare two strings, facilitating sorting of characters in a string.

3. sortString Function: This function takes a string and sorts its characters using the standard library function qsort.

4. groupAnagrams Function: This is the core function that processes the list of strings. It sorts each string, groups anagrams together, and prints them.

  • It first creates an array to store the sorted versions of the input strings.
  • Then it groups the original strings based on their sorted versions.
  • Finally, it prints each group of anagrams.

5. Main Function: This initializes an array of strings and calls the groupAnagrams function to display the grouped anagrams.

Conclusion

The program effectively groups anagrams by sorting the strings and using a simple comparison method. This solution demonstrates the use of fundamental C programming concepts, including arrays, strings, and dynamic memory allocation.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)