This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
user:kkrauss1:portfolio:palindrome [2011/12/15 01:29] – kkrauss1 | user:kkrauss1:portfolio:palindrome [2011/12/15 01:33] (current) – kkrauss1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | *This is a palindrome program that will take one word entered at the command line and test to see if it is a palindrome. | ||
+ | *This program was above and beyond anything I " | ||
+ | <code c> | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | |||
+ | |||
+ | int main(int argc, char *argv[])// I chose to allow for command line interface | ||
+ | { | ||
+ | if(argc != 2)// when compiling you will get a warning that 0 or 2 argument is all that is allowed but | ||
+ | { //this is just to make sure user enters one and only one word to test | ||
+ | printf(" | ||
+ | exit (1); | ||
+ | } | ||
+ | |||
+ | Stack *myStack, *myStackBackup, | ||
+ | Node *tmp, *tmp2;// Temps are needed for the various times the pop function is called. | ||
+ | myStack = (Stack *)malloc(sizeof(Stack)); | ||
+ | myStackBackup = (Stack *)malloc(sizeof(Stack)); | ||
+ | kcatSmy = (Stack *)malloc(sizeof(Stack)); | ||
+ | myStack-> | ||
+ | int i = 0; | ||
+ | |||
+ | printf(" | ||
+ | while (argv[1][i] != ' | ||
+ | { | ||
+ | push(myStack, | ||
+ | push(myStackBackup, | ||
+ | printf(" | ||
+ | i++; | ||
+ | } | ||
+ | |||
+ | printf(" | ||
+ | tmp = pop(myStackBackup); | ||
+ | while(tmp!= NULL)// this test lets us know if the stack is been popped until empty | ||
+ | { | ||
+ | push(kcatSmy, | ||
+ | printf(" | ||
+ | tmp = pop(myStackBackup);// | ||
+ | } | ||
+ | printf(" | ||
+ | // So now we have to stacks, myStack and kcatSmy, now we need to compare and see if they are the same | ||
+ | tmp = pop(myStack);// | ||
+ | tmp2 = pop(kcatSmy); | ||
+ | while(tmp != NULL)// since at this point we know both stacks are the same size you can test either tmp for NULL | ||
+ | { | ||
+ | if (tmp-> | ||
+ | { | ||
+ | printf(" | ||
+ | exit (0); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | tmp = pop(myStack);// | ||
+ | tmp2 = pop(kcatSmy); | ||
+ | } | ||
+ | } | ||
+ | printf(" | ||
+ | |||
+ | return (0); | ||
+ | } | ||
+ | </ |