aboutsummaryrefslogtreecommitdiffstats
path: root/revlist.c
blob: dd473c01fc7434be239c85a3684cad3fd6d32fdf (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
/*
 * 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 <string.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    if(argc != 3){
        fprintf(stderr, "Incorrect argument count.\nUsage: %s <string> <separator>\n"\
               , argv[0]);
        exit(1);
    }

    char *input = argv[1];
    char separator = argv[2][0];
    int length = strlen(input), startpos = 0, strpos;

    char *output = malloc(length);

    for(strpos = 0; strpos <= length; strpos ++){
        if(input[strpos] == separator || strpos == length){
            strncpy(output + length - strpos, input + startpos, strpos - startpos);
            if(strpos != length)
                output[length - strpos - 1] = separator;
            startpos = strpos + 1;
        }
    }

    printf("%s\n", output);
    free(output);
    return 0;
}