Skip to content

Commit

Permalink
Create getFirstNonRepeatedString.c
Browse files Browse the repository at this point in the history
  • Loading branch information
saksham101 authored Jan 6, 2021
1 parent 585fa0d commit 7acc76b
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions getFirstNonRepeatedString.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
#define N 256

int* element_count(char* str)
{
int* count = (int*)calloc(
sizeof(int), N);
int i;
for (i = 0; *(str + i); i++)
count[*(str + i)]++;
return count;
}

int firstNonRepeat(char* str)
{
int* count = element_count(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[10000];
scanf("%s", str);
int index = firstNonRepeat(str);
if (index == -1)
printf("_");
else
printf("%c", str[index]);
getchar();
return 0;
}

0 comments on commit 7acc76b

Please sign in to comment.