aboutsummaryrefslogtreecommitdiffstats
path: root/split3.c
blob: e9b98e0633d1bc0fb5bdb21bce2ae10bf293d0c1 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Copyright (C) 2014  Tomasz Kramkowski <tk@the-tk.com>
 *
 * This program is free software. It is licensed under version 3 of the
 * GNU General Public License.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
 */

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

int *genccountdict(char *);
char *nodup(char *, int *);
char *nnodup(char *, int *);
char *dup(char *, int *);

int main(int argc, char **argv)
{
    int i;
    if(argc!=2)
        exit(1);

    char *string = *(argv+1);
    int *dict = genccountdict(string);

    for(i = 0; i < strlen(string); i++)
        string[i] = toupper(string[i]);

    printf("nodup: %s\n", nodup(string, dict));
    printf("nnodup: %s\n", nnodup(string, dict));
    printf("dup: %s\n", dup(string, dict));
    return 0;
}

int *genccountdict(char *input)
{
    int *dict = calloc(sizeof(int), 26);
    int i;
    for(i=0; i<strlen(input); i++)
        if(isalpha(input[i]))
            dict[toupper(input[i])-'A']++;
    return dict;
}

char *nodup(char *input)
{
    char *output = malloc(strlen(input)+1);
    int i, outpt = 0;

    for(i=0; i<strlen(input); i++){
        if(!dict[input[i]-'A']){
            output[outpt++]=input[i];
            dict[input[i]-'A'] = 1;
        }
    }
    output[outpt]='\0';
    return output;
}

char *nnodup(char *input, int *dict)
{
    char *output = malloc(strlen(input)+1);
    int i, outpt = 0;
    for(i=0; i<strlen(input); i++)
        if(dict[input[i]-'A']==1)
            output[outpt++]=input[i];
    output[outpt]='\0';
    return output;
}

char *dup(char *input, int *dict)
{
    char *output = malloc(strlen(input)+1);
    int i, outpt = 0;
    for(i=0; i<strlen(input); i++)
        if(dict[input[i]-'A']>1)
            output[outpt++]=input[i];
    output[outpt] = '\0';
    int ndict[26];
    memset(ndict, 0, 26*sizeof(int));
    return nodup(output, ndict);
}