distinct char string c 2b 2b

Solutions on MaxInterview for distinct char string c 2b 2b by the best coders in the world

showing results for - "distinct char string c 2b 2b"
Maritza
28 Aug 2017
1#include<stdio.h>
2#include<string.h>
3
4// function to return the number of unique
5// characters in str[]
6int count_unique_char(char* str) {
7
8	int hash[128] = { 0 };
9	int i, c = 0;
10
11	// reading each character of str[]
12	for (i = 0; i < strlen(str); ++i) {
13		// set the position corresponding 
14		// to the ASCII value of str[i] in hash[] to 1
15		hash[str[i]] = 1;
16	}
17
18	// counting number of unique characters
19	// repeated elements are only counted once
20	for (i = 0; i < 128; ++i) {
21		c += hash[i];
22	}
23
24	return c;
25
26}
27
28int main() {
29
30	char str[300];
31
32	printf("Enter String: ");
33	gets(str);
34
35	printf("Number of Unique Characters in String: %d", count_unique_char(str));
36
37}
38