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

/* functies wissel en reverse zoals besproken in de les maar dan aangepast om te werken met char i.p.v. int */

void wissel(char *p, char *q) {
    char hulpje = *p;
    *p = *q;
    *q = hulpje;
}

void reverse(char a[]) {
    int first = 0, last = strlen(a) - 1;
    while (first < last) {
        wissel(&a[first], &a[last]);
        first = first + 1;
        last = last - 1;
    }
}

/* onderstaande implementatie van isPalindroom werkt alleen in C99 (en dus niet in Microsoft Visual C 2010) */

int isPalindroom(char string[]) {
    char kopie[strlen(string) + 1];
    strcpy(kopie, string);
    reverse(kopie);
    return strcmp(string, kopie) == 0;
}

int main(void) {
    int errors = 0, i;
    char* testStrings[] = { "ABBA", "ABA", "maandnaam", "", "ABB", "ABBB", "ABAA"};
    int expectedResults[] = {    1,     1,           1,  1,     0,      0,      0};  
    for (i = 0; i < sizeof testStrings / sizeof testStrings[0]; i = i + 1) {
        if (isPalindroom(testStrings[i]) != expectedResults[i]) {
            printf("Test isPalindroom(\"%s\") FAALT\n", testStrings[i]);
            errors = errors + 1;
        }
    }
    printf("%d tests GEFAALD!\n", errors);
    getchar();
    return 0;
}
