palindrome.c - OpenLearning
//palindrome.c
//Unofficial Practice Practical Exam
//Created by Michael Simarta
//Your Name Here
 
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
 
#define FALSE 0
#define TRUE 1
#define START_LETTER 'a'
#define END_LETTER 'z'
 
void testCase(void);
int palindrome (char *string);
 
int main (int argc, char *argv[]) {
    testCase();
    return EXIT_SUCCESS;
}
 
int palindrome (char *string) {
    //YOUR CODE HERE
    int isPalindrome = TRUE;
    char *reversed = malloc (sizeof (string));
    char *striped = malloc (sizeof (string));

    int count = 0;
    while (count < strlen (string) - 1) {
        if (string[count] < END_LETTER &&
            string[count] > START_LETTER) {

            snprintf (striped, sizeof (striped), "%c", string[count]);
        }
        count++;
    }

    count = strlen (string) - 1;
    while (count >= 0) {
        if (string[count] < END_LETTER &&
            string[count] > START_LETTER) {

            snprintf (reversed, sizeof (striped), "%c", string[count]);
        }
        count--;
    }

    //printf("%s %s\n", striped, reversed);

    count = 0;
    while (count < strlen (striped)) {
        if (striped[count] != reversed[count]) {
            isPalindrome = FALSE;
        }
        count++;
    }

    free (reversed);
    free (striped);

    return isPalindrome;
}
 
void testCase(void) {
 
    assert (palindrome("kayak.") == TRUE);
    assert (palindrome("puzzlequest") == FALSE);
    assert (palindrome("is addo odd as i?") == TRUE);
    assert (palindrome("canoe") == FALSE);
    assert (palindrome("eevee") == TRUE);
    assert (palindrome("awesome") == FALSE);
 
    //ADD MORE TESTS
    assert (palindrome("asdfjkjkjkjfdsa") == TRUE);
    assert (palindrome("a;dlskjf;kaljsdk;flj") == FALSE);
    assert (palindrome("aaaaaaaaaaaaaa") == TRUE);
    assert (palindrome("what is going on here") == FALSE);
    assert (palindrome("Ilik2ec1ode4edoc!eki5lI") == TRUE);
    assert (palindrome("cool cool") == FALSE);

    printf("All tests passed, you are Awesome!\n");
}

Download file: palindrome.c (2.0 KB)

Comments

Chat