//palindromeStack.c //Unofficial Practice Practical Exam //Created by Michael Simarta //Your Name Here //Multi-file Compilation: gcc -Wall -Werror -O -o PROGRAMNAME palindromeStack.c Stack.c #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h> #include "Stack.h" #define FALSE 0 #define TRUE 1 #define START_LETTER 'a' #define END_LETTER 'z' void testCase(void); int palindromeStack (char* string); int main (int argc, char *argv[]) { testCase(); return EXIT_SUCCESS; } int palindromeStack (char* string) { //Hint: You may need more than 1 stack. //YOUR CODE HERE int isPalindrome = TRUE; Stack stringIn = newStack (); int count = 0; while (count <= strlen (string)) { if ((string[count] <= END_LETTER) && (string[count] >= START_LETTER)) { push (stringIn, string[count]); } count++; } count = 0; while (count < strlen (string)) { if ((string[count] <= END_LETTER) && (string[count] >= START_LETTER) && (length (stringIn) > 0)) { if (top (stringIn) != string[count]) { isPalindrome = FALSE; } pop (stringIn); } count++; } free (stringIn); return isPalindrome; } void testCase(void) { assert (palindromeStack("kayak.") == TRUE); assert (palindromeStack("puzzlequest") == FALSE); assert (palindromeStack("is addo odd as i?") == TRUE); assert (palindromeStack("canoe") == FALSE); assert (palindromeStack("eevee") == TRUE); assert (palindromeStack("awesome") == FALSE); //ADD MORE TESTS assert (palindromeStack("asdfjkjkjkjfdsa") == TRUE); assert (palindromeStack("a;dlskjf;kaljsdk;flj") == FALSE); assert (palindromeStack("aaaaaaaaaaaaaa") == TRUE); assert (palindromeStack("what is going on here") == FALSE); assert (palindromeStack("Ilik2ec1ode4edoc!eki5lI") == TRUE); assert (palindromeStack("cool cool") == FALSE); printf("All tests passed, you are Awesome!\n"); }
Download file: palindromeStack.c
(2.0 KB)