aboutsummaryrefslogtreecommitdiffstats
path: root/revlist.c
blob: 4c5dddb8016baf26d922b26a8d772cc91b2d111d (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
#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;
}