User Tools

Site Tools


user:dschoeff:portfolio:palindrome

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/perform this project, the listed resources/experiences need to be consulted/achieved:

  • 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

/***********************************************************
* 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: ./palindrome                               *
*                                                          *
***********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
 
int count(char *);
 
int main()
{
   //declaration of 3 stacks that will be used to determine if a string is a palindrome
   Stack *stack1;
   Stack *stack2;
   Stack *stack3;
 
   stack1 = stack2 = stack3 = NULL;//initialize them to NULL
 
   //The max size string allowed is 50 characters
   char *ch;
   ch  =  malloc(sizeof(char)*50);
   int length, i, flag = 0;
   printf("Enter a string: ");
   scanf("%s",ch);
 
   length = count(ch);// function count() returns the length of the string given
 
   for(i = 0; i < length; i++)//for loop loads the string into stack1
   {
      stack1 = push(stack1, *(ch+i));
   }
   for(i = 0; i < length; i++)//for loop loads string into stack3
   {
      stack3 = push(stack3, *(ch+i));
   }
   for(i = 0; i < length; i++)//for loop loads the values from stack one into
   {                          // stack2 in reverse order
      stack2 = push(stack2,pop(stack1));
   }
   for(i = 0; i < length; i++)//for loop loads the string back into stack1
   {
      stack1 = push(stack1,*(ch+i));
   }
   //this for loop pops values from stack1 and stack2. if at any point there
   //is an inequality, flag is set to 1 indicating that the string is not a
   //palindrome.
   for( i = 0; i < length; i++)
   {
      if(pop(stack1) != pop(stack2))
      {
         flag = 1;
      }
   }
   //prints out the string in order and in reverse order
   printf("Before: %s\n", ch);
   printf("After: ");
   for( i = 0; i < length; i++)
   {
      printf("%c",pop(stack3));
   }
   //prints whether the string is a palindrome or not.
   printf("\n\"%s\"", ch);
   if(flag == 1)
   {
      printf(" is not a palindrome.\n");
   }
   else
   {
      printf(" is a palindrome.\n");
   }
   return (0);
}
// 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;
   while(string[letterCount] != '\0')
   {
      letterCount++;
   }
   return letterCount;
}

Execution

In the execution of my code, I ran the program twice entering different values each time.

lab46:~/src/data/eoce/0x1$ ./palindrome
Enter a string: racecar
Before: racecar
After: racecar
"racecar" is a palindrome.
lab46:~/src/data/eoce/0x1$ ./palindrome
Enter a string: datastructures
Before: datastructures
After: serutcurtsatad
"datastructures" is not a palindrome.
lab46:~/src/data/eoce/0x1$ 

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:

user/dschoeff/portfolio/palindrome.txt · Last modified: 2011/12/13 03:22 by dschoeff