User Tools

Site Tools


user:kkrauss1:portfolio:palindrome

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
user:kkrauss1:portfolio:palindrome [2011/12/15 01:29] kkrauss1user: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 "needed" to do but I wanted to have it in my portfolio.  It covers a lot of attributes.  Command line arguments, stacks, pointers, selection etc.
  
 +<code c>
 +#include<stdio.h>
 +#include<stdlib.h>
 +#include"stack.h"
 +
 +
 +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("Please enter only one word to check at a time\n");
 + exit (1);
 + }
 +
 + Stack *myStack, *myStackBackup, *kcatSmy;// myStack is original word, Backup is needed to created kcatSmy which is mystack values backwards
 + 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->start = myStack->end = myStackBackup->start = myStackBackup->end = kcatSmy->start = kcatSmy->end = tmp = tmp2 = NULL;
 + int i = 0;
 +
 + printf("Before: ");
 + while (argv[1][i] != '\0') // this tests for the end of the string the user entered
 + {
 + push(myStack, (int)argv[1][i]);
 + push(myStackBackup, (int)argv[1][i]); // these lines copy the string from argv into myStack and prints the values
 + printf("%c", myStack->end->value);
 + i++;
 + }
 +
 + printf("\nAfter: ");
 + tmp = pop(myStackBackup); //This pops the value off of myStackBackup and stores the value in the tmp node
 + while(tmp!= NULL)// this test lets us know if the stack is been popped until empty
 + {
 + push(kcatSmy, tmp->value); //this copies each popped value from myStackBackup into kcatSmy
 + printf("%c", kcatSmy->end->value);//this prints the values backwards.
 + tmp = pop(myStackBackup);// and now we pop again, this is done until the stack is empty
 + }
 + printf("\n");
 + // So now we have to stacks, myStack and kcatSmy, now we need to compare and see if they are the same
 + tmp = pop(myStack);// we will pop both stacks
 + 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->value != tmp2->value)// as soon as any compared nodeds are not equal we know its not a palindrome so we can exit.
 + {
 + printf("This is not a palindrome.\n");
 + exit (0);
 + }
 + else
 + {
 + tmp = pop(myStack);// if the compared nodes are equal we need to move through the list and check each one
 + tmp2 = pop(kcatSmy);
 + }
 + }
 + printf("This IS a palindrome!\n");// if we make it through the list and all nodes are equal then we know its a palindrome!
 +
 + return (0);
 +}
 +</code>