count distinct characters in a string C

To count the distinct characters in a string in C, you can use an array to keep track of the frequency of each character. Here's an example code snippet:

#include <stdio.h>

#define MAX_SIZE 256

int countDistinctChars(char* str) {
    int count[MAX_SIZE] = {0};
    int distinctCount = 0;

    // Traverse the string and update the count array
    for (int i = 0; str[i] != '\0'; i++) {
        count[str[i]]++;
    }

    // Count the number of distinct characters
    for (int i = 0; i < MAX_SIZE; i++) {
        if (count[i] > 0) {
            distinctCount++;
        }
    }

    return distinctCount;
}

int main() {
    char str[] = "Hello, World!";
    int distinctCount = countDistinctChars(str);
    printf("Number of distinct characters: %d\n", distinctCount);

    return 0;
}

In this code, we define an array count of size 256 (assuming ASCII characters) to store the frequency of each character. We initialize all elements of the array to 0. Then, we traverse the string and update the count array accordingly. Finally, we count the number of elements in the count array that are greater than 0, which gives us the number of distinct characters in the string.

Please note that this code assumes ASCII characters. If you're working with a different character encoding, you may need to adjust the size of the count array accordingly.

I hope this helps! Let me know if you have any further questions.