This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
user:dschoeff:portfolio:palindrome [2011/12/13 02:33] – dschoeff | user:dschoeff:portfolio:palindrome [2011/12/13 03:22] (current) – [Attributes] dschoeff | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Palindrome Program====== | ||
+ | |||
+ | A project for Data Structures by Dave Schoeffler during the Fall 2011 semester. | ||
+ | |||
+ | This project was begun on December 4th and was finished on December 5th. | ||
+ | |||
+ | =====Objectives===== | ||
+ | This project will utilize my Stack Library in creating a program that will check a string to determine whether it is a palindrome or not. | ||
+ | |||
+ | |||
+ | |||
+ | =====Prerequisites===== | ||
+ | In order to successfully accomplish/ | ||
+ | |||
+ | * Understanding the C programming language | ||
+ | * Be familiar with my Stack library | ||
+ | |||
+ | |||
+ | =====Background===== | ||
+ | In the course of my computer programming experience, I have done a couple different palindrome programs. One of those used | ||
+ | array indexing while the other used recursion. This time I will be using stacks to do this task. | ||
+ | |||
+ | =====Scope===== | ||
+ | My program will function as follows. I will accept a string from the user. I will then utilize my Stack Library | ||
+ | to find whether the string is a palindrome or not. I will push() the string I get into two stacks. I will then pop() | ||
+ | one of the stacks into a 3rd stack. Now stack one and three contain the string forwards and backwards. I will then pop() | ||
+ | off the top nodes of each of the stacks comparing the values each time. If ever there is a time when the two | ||
+ | popped nodes are not equal, I know that the string is not a palindrome. | ||
+ | |||
+ | I will then display the results to the user. | ||
+ | |||
+ | =====Attributes===== | ||
+ | The following attributes will be used in my program. | ||
+ | |||
+ | * pointers: A pointer will be used to mark the beginning of the array | ||
+ | * malloc/new: You must allocate memory for the array | ||
+ | * Stacks: this program utilize my stack library | ||
+ | * linked lists: my stack library utilizes linked lists | ||
+ | * libraries: This program uses a non core libc library | ||
+ | |||
+ | |||
+ | |||
+ | =====Code===== | ||
+ | ===palindrome.c=== | ||
+ | <code c> | ||
+ | / | ||
+ | * Author: Dave Schoeffler | ||
+ | * This program will will check to see whether a given * | ||
+ | * string is a palindrome. It will utilize my own stack.h | ||
+ | * header file to do this. * | ||
+ | * * | ||
+ | * Compile with: gcc -o palindrome palindrome.c -lstack -L. * | ||
+ | * * | ||
+ | * Execute with: ./ | ||
+ | * * | ||
+ | ***********************************************************/ | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | int count(char *); | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | // | ||
+ | Stack *stack1; | ||
+ | Stack *stack2; | ||
+ | Stack *stack3; | ||
+ | |||
+ | | ||
+ | |||
+ | //The max size string allowed is 50 characters | ||
+ | char *ch; | ||
+ | | ||
+ | int length, i, flag = 0; | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | |||
+ | for(i = 0; i < length; i++)//for loop loads the string into stack1 | ||
+ | { | ||
+ | stack1 = push(stack1, | ||
+ | } | ||
+ | for(i = 0; i < length; i++)//for loop loads string into stack3 | ||
+ | { | ||
+ | stack3 = push(stack3, | ||
+ | } | ||
+ | for(i = 0; i < length; i++)//for loop loads the values from stack one into | ||
+ | | ||
+ | stack2 = push(stack2, | ||
+ | } | ||
+ | for(i = 0; i < length; i++)//for loop loads the string back into stack1 | ||
+ | { | ||
+ | stack1 = push(stack1, | ||
+ | } | ||
+ | // | ||
+ | //is an inequality, flag is set to 1 indicating that the string is not a | ||
+ | // | ||
+ | for( i = 0; i < length; i++) | ||
+ | { | ||
+ | if(pop(stack1) != pop(stack2)) | ||
+ | { | ||
+ | flag = 1; | ||
+ | } | ||
+ | } | ||
+ | // | ||
+ | | ||
+ | | ||
+ | for( i = 0; i < length; i++) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | // | ||
+ | | ||
+ | | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | // function count() receives as input a string and returns the number | ||
+ | // of characters in that string as an int | ||
+ | int count(char *string) | ||
+ | { | ||
+ | int letterCount = 0; | ||
+ | | ||
+ | { | ||
+ | letterCount++; | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | =====Execution===== | ||
+ | In the execution of my code, I ran the program twice entering different values each time. | ||
+ | <cli> | ||
+ | lab46: | ||
+ | Enter a string: racecar | ||
+ | Before: racecar | ||
+ | After: racecar | ||
+ | " | ||
+ | lab46: | ||
+ | Enter a string: datastructures | ||
+ | Before: datastructures | ||
+ | After: serutcurtsatad | ||
+ | " | ||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | =====Reflection===== | ||
+ | Doing this palindrome project has been a cool experience. I have been able to practice using a stack library that I created which was pretty cool. | ||
+ | I didn't find to many challenges in this project which I think has to do with being more familiar with linked data structures. | ||
+ | |||
+ | Overall this was a fun little project to do. | ||
+ | =====References===== | ||
+ | In performing this project, the following resources were referenced: | ||
+ | |||
+ | * [[http:// | ||
+ | |||