-
Notifications
You must be signed in to change notification settings - Fork 320
/
ch-1.c
52 lines (44 loc) · 829 Bytes
/
ch-1.c
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
41
42
43
44
45
46
47
48
49
50
51
52
//
// Task 1: First Unique Character
//
// C version.
//
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "args.h"
//
// int pos = fuc_pos( s );
// Find the position of the first unique character in the string s,
// or -1 if all characters in s are repeated.
//
int fuc_pos( char *s )
{
int freq[256]; // array over char of int
for( int i=0; i<256; i++ ) freq[i] = 0;
for( char *p = s; *p; p++ )
{
freq[(int)*p]++;
}
int pos = 0;
for( char *p = s; *p; p++, pos++ )
{
if( freq[(int)*p] == 1 )
{
return pos;
}
}
return -1;
}
int main( int argc, char **argv )
{
int argno = process_flag_n_args( "fuc", argc, argv,
1, "String" );
char *str = argv[argno];
int pos = fuc_pos( str );
printf( "%d\n", pos );
return 0;
}