-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ4
More file actions
40 lines (35 loc) · 798 Bytes
/
Q4
File metadata and controls
40 lines (35 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using namespace std;
int* getCharCountArray(char* str)
{
int* count = (int*)calloc(sizeof(int), NO_OF_CHARS);
int i;
for (i = 0; *(str + i); i++)
count[*(str + i)]++;
return count;
}
int firstNonRepeating(char* str)
{
int* count = getCharCountArray(str);
int index = -1, i;
for (i = 0; *(str + i); i++) {
if (count[*(str + i)] == 1) {
index = i;
break;
}
}
free(count);
return index;
}
int main()
{
char str[] = "geeksforgeeks";
int index = firstNonRepeating(str);
if (index == -1)
cout << "Either all characters are repeating or "
"string is empty";
else
cout << "First non-repeating character is "<<
str[index];
getchar();
return 0;
}