I've got some of my project finished. This is supposed to 1) check to see if each word in the input file is a palindrome using stacks
2) If the word is a palindrome, write the word to a separate output filr.
3) Count the number of words read and the number of words which are found to be palindromes.

Here is my main.c file

#include<stdio.h>
#include "Palindrome.h"
#define SIZE 300


int main(void)
{
	char txt[500];

	readFile(txt);
	writeFile(txt);
	checkPalindrome();


	return 0;
}

This is my function definition.c file. Now should I use stacks to check for palindromes in the current functions? or should I make a whole new function for that?

#include<stdio.h>
#include "Palindrome.h"
#define SIZE 300


void readFile(char txt[])
{
    char txtname[100];

    FILE *rfPtr;

    printf("Please enter a file to read for palindromes.\n");
    scanf("%s",txtname);

	rfPtr=fopen(txtname,"r");

    if (rfPtr==NULL)
    {
            printf("File cannot be opened!\n");
    }
    else
    {
        while(!feof(rfPtr))
		{           
        fscanf(rfPtr,"%s",txt);
        printf("%s ",txt);
		}
    }
    fclose(rfPtr);

    return;
}


void writeFile(char txt[])
{
	FILE *cfPtr;

    cfPtr=fopen("J:\\Palindrome\\Debug\\output.txt","w");


    if(cfPtr==NULL)
    {
        printf("File cannot be opened!\n");
    }
    else
	{ 
        fprintf(cfPtr,"%s ",txt);
    }
   
    fclose(cfPtr);

	return;
}

and here is my palindrome.h file

#include <stdio.h>

void readFile(char txt[]);
void writeFile(char txt[]);
//void checkPalindrome();

also if you try to run it it doesn't read from the file it reads the enter key from the keyboard only

Your structure is wrong. Your main should be something like this:

int main (void) {
    /* Init count of words and count of palindromes to zero */
    /* Open files */
    /* While reading next word does not give EOF */
        /* Increment count of words */
        /* If the word is a palindrome */
           /* Increment count of palindromes */
           /* Ouput to palindrome file */
    /* Show results */
    /* Close files */
}

This is now what I have. The word count is correct, however I can't get the palindromes to count. The output says--" " is a palindrome--

The help given already has helped by leaps and bounds.

#include<stdio.h>
#include "Palindrome.h"
#define SIZE 300


int main(void)
{
	char txt[SIZE];
	char *s;
	int wordCount=0;//word count
	int palCount=0;	//palindrome count
    char strsrc[SIZE];
	char strtmp[SIZE];

	FILE *rfPtr;

    /* Open file */
    printf("Please enter a file to read for palindromes.\n");
    scanf("%s",txt);
	gets(strsrc);
	s = strtok (txt, ", ! .");

	rfPtr=fopen(txt,"r");

    if (rfPtr==NULL)
    {
            printf("File cannot be opened!\n");
    }
    else
    {
  	/* While reading next word does not give EOF */
      while(!feof(rfPtr))
		{           
        fscanf(rfPtr,"%s",txt);
        printf("%s ",txt);
		wordCount++;	/* Increment count of words */
		}

	  strcpy(strtmp,strsrc);
	  strrev(strtmp);
	  
	  if(strcmp(strsrc,strtmp)==0)
	  {
		  printf("\n Entered string \"%s\" is a palindrome",strsrc);
		  palCount++;
	  }
	  else
	  {
		  printf("\n Entered string \"%s\" is not a palindrome",strsrc);
		  getch();
	  }
        /* If the word is a palindrome */
        /* Increment count of palindromes */

    }
    fclose(rfPtr);

	printf("\n\nThe word count is: %d\n",wordCount);
	printf("The palindrome count is: %d\n\n",palCount);
		
           /* Ouput to palindrome file */
    /* Show results */
    /* Close files */



	return 0;
}

i tried to compile and i got this result:

rob@rob:~/Desktop/work$ gcc palindrome.c
/tmp/cc2AgRo2.o: In function `main':
palindrome.c:(.text+0x5f): warning: the `gets' function is dangerous and should not be used.
palindrome.c:(.text+0x126): undefined reference to `strrev'
palindrome.c:(.text+0x17c): undefined reference to `getch'
collect2: ld returned 1 exit status

two problems

(1) gets? now why would do such a horrible thing? hasn't anyone told you?
(2) strrev and getch are not standard C.

the first is a warning, but the second is a show stopper. do you think someone is going to want to rewrite your code just to make it compile?

Your first attempt was much better. Writing functions is the way to go, as you were doing. Don't follow nucleon's advice.

Do not use scanf() to read strings. Here's why.
Don't use feof() either. Here's why.

Tell us the following:
1) What is a stack?
2) How can a stack be used to test for palindromeness.

If you can answer these two questions, how did your program implement the stack?

Your first attempt was much better. Writing functions is the way to go, as you were doing. Don't follow nucleon's advice.

This is either a joke or he misread your original code. My advice is certainly not to NOT write functions! Of course you should write functions!!! I was just giving a program structure that would actually work.

^ "git-r-dun" is not a recommended design process, and you will find little support for you if you recommend it.

.

^ Non sequitur.

WaltP misread (or did not fully read) the original code, which would simply not accomplish the task. It was the wrong structure, perhaps of the "git-er-done" type, I don't know.

It basically said
* Do all input
* Do all output
whereas the natural structure for the task is
* Open input and output files
* Process files
* Close files

It was certainly not my intent that all of that logic was to be implemented inline. I guess that's what WaltP was talking about. I suppose it does look like I meant it all to be inline, which I didn't really mean. I just meant to indicate a general algorithm that would accomplish the task.

It is left as a (further) exercise to the OP to divide it into appropriate functions. :)

Thanks for the help guys. I'm about to start on this again and probably will make another post. I just got off work and will be at this until about 2 am central time. This is one of the best sites and I thank everyone for their information on helping me learn.

Ok. I'm way closer than I was but still need some help. I'll place my code below for the 2 .c files and the .h file. I'm needing to get it to take out the punctuation and write the palindromes to an output.txt file.

main.c

#include <stdio.h>
#include "PalindromeDefs.h"
int main(void)
{
	readFile();
	return 0;
}

PalindromeDefs.c

#include <stdio.h>
#include "PalindromeDefs.h"
#define SIZE 100

void readFile()
{
	Stack A;
	//CreateStack(*A);
	int wordCount=0;
	int palindromeCount=0;
	int i = 0;
	int counter = 0;
	char *token;
	char txt[SIZE];
	char txtname[SIZE];
	char checkString[SIZE];
	
    FILE *rfPtr;
	

    puts("Enter a directory path and we will check it for palindromes.\n");
    scanf("%s[^\n]",txtname);

	rfPtr=fopen(txtname,"r");

    if (rfPtr==NULL)
    {
            printf("File cannot be opened!\n");
    }
    else
    {
		while(fgets(txt,200,rfPtr)!=NULL)
		{
		token=strtok(txt," .\n");
		while(token!=NULL)
		{
			fprintf(rfPtr,"%s\n",token);
			
			token=strtok(NULL," .\n");
			sscanf(token, "%s", txt[i]);
				i++;
		}
			checkString=strupr(strrev(txt));
			txt=getStringFromStack(A,counter);			
			if(isPalindrome(txt,strrev(txt))==1) 
		{
				writeFile(txt);
				palindromeCount++;
			}
			wordCount++;
		}
    }
	printf("\n\nThe number of words is %d\nThe number of palindromes is %d\n\n",wordCount,palindromeCount);
	fclose(rfPtr);
    return;
}


/*char getStringFromStack(Stack C, int counter)
{
	char temporary[counter];
	int place = 0;
	while(counter!=-1)
	{
		temporary[place]=Pop(&value,&A);
	}
	return temporary;
}*/

void writeFile(char txt[])
{
	FILE *cfPtr;

	cfPtr=fopen("c:\\output.txt","a");


    if(cfPtr==NULL)
    {
        printf("File cannot be opened!\n");
    }
    else
	{ 
        fprintf(cfPtr,"%s ",txt);
    }

    fclose(cfPtr);


	return;
}

/* CreateStack: will initalize the stack to empty			*/
/*		preconditions: Stack variable declared				*/
/*		postconditions: Stack will be initialized and empty */
void CreateStack(Stack *s)
{	
	s->top=0;//' ' maybe
	return;
}

/* Push: puts an item onto the stack						*/
/*		pre: Stack exists and is not full	                */
/*		post: item will be put on the top of stack          */
void Push(StackEntry item,Stack *s)
{
	if(StackFull(s))
		puts("Error : A Push to a full stack has been attempted");
	else
	{	s->entry[s->top]= item;
		s->top =s->top +1;
	}
	return;
}




/* Pop: pops an item from the stack						      */
/*		pre: Stack exists and is not empty	                  */
/*		post: Top of stack has been removed & stored in item  */
void Pop(StackEntry *item,Stack *s)
{
	if(StackEmpty(s))
		puts("Error : A pop from an empty stack has been attempted");
	else
	{	
		s->top=s->top-1;
		*item=s->entry[s->top];
	}
	return;
}

/* StackEmpty: tests if stack is empty or not				*/
/*		pre: Stack exists and is initialized                */
/*		post: returns TRUE (if empty) or FALSE(if nonempty) */
void isPalindrome(char strsrc[], char strtmp[])
{
	strcpy(strtmp,strsrc);
		
	if(strcmp(strsrc,strtmp)==0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

and PalindromeDefs.h

#include <stdio.h>
#ifndef MYSTACK
#define MYSTACK

#define MAXSTACKSIZE 10
typedef char StackEntry; //change to char
typedef enum {FALSE=0, TRUE=1} Boolean;
typedef struct stack
{	int top;
	StackEntry entry[MAXSTACKSIZE];
} Stack;   // Now we can declare variables of type Stack

/* CreateStack: will initalize the stack to empty			*/
/*		preconditions: Stack variable declared				*/
/*		postconditions: Stack will be initialized and empty */
void CreateStack(Stack *s);

/* Push: puts an item onto the stack						*/
/*		pre: Stack exists and is not full	                */
/*		post: item will be put on the top of stack          */
void Push(StackEntry item,Stack *s);

/* Pop: pops an item from the stack						      */
/*		pre: Stack exists and is not empty	                  */
/*		post: Top of stack has been removed & stored in item  */
void Pop(StackEntry *item,Stack *s);

/* StackEmpty: tests if stack is empty or not				*/
/*		pre: Stack exists and is initialized                */
/*		post: returns TRUE (if empty) or FALSE(if nonempty) */
Boolean StackEmpty(Stack *s);	 //pass by address here is for efficiency

/* StackFull: tests if stack is empty or not				*/
/*		pre: Stack exists and is initialized                */
/*		post: returns TRUE (if empty) or FALSE(if nonempty) */
Boolean StackEmpty(Stack *s);	

#endif

void readFile();
void writeFile(char txt[]);
void ifPalindrome();

Ok. I'm way closer than I was but still need some help. I'll place my code below for the 2 .c files and the .h file. I'm needing to get it to take out the punctuation and write the palindromes to an output.txt file.

main.c

#include <stdio.h>
#include "PalindromeDefs.h"
int main(void)
{
	readFile();
	return 0;
}

PalindromeDefs.c

#include <stdio.h>
#include "PalindromeDefs.h"
#define SIZE 100

void readFile()
{
	Stack A;
	//CreateStack(*A);
	int wordCount=0;
	int palindromeCount=0;
	int i = 0;
	int counter = 0;
	char *token;
	char txt[SIZE];
	char txtname[SIZE];
	char checkString[SIZE];
	
    FILE *rfPtr;
	

    puts("Enter a directory path and we will check it for palindromes.\n");
    scanf("%s[^\n]",txtname);

	rfPtr=fopen(txtname,"r");

    if (rfPtr==NULL)
    {
            printf("File cannot be opened!\n");
    }
    else
    {
		while(fgets(txt,200,rfPtr)!=NULL)
		{
		token=strtok(txt," .\n");
		while(token!=NULL)
		{
			fprintf(rfPtr,"%s\n",token);
			
			token=strtok(NULL," .\n");
			sscanf(token, "%s", txt[i]);
				i++;
		}
			checkString=strupr(strrev(txt));
			txt=getStringFromStack(A,counter);			
			if(isPalindrome(txt,strrev(txt))==1) 
		{
				writeFile(txt);
				palindromeCount++;
			}
			wordCount++;
		}
    }
	printf("\n\nThe number of words is %d\nThe number of palindromes is %d\n\n",wordCount,palindromeCount);
	fclose(rfPtr);
    return;
}


/*char getStringFromStack(Stack C, int counter)
{
	char temporary[counter];
	int place = 0;
	while(counter!=-1)
	{
		temporary[place]=Pop(&value,&A);
	}
	return temporary;
}*/

void writeFile(char txt[])
{
	FILE *cfPtr;

	cfPtr=fopen("c:\\output.txt","a");


    if(cfPtr==NULL)
    {
        printf("File cannot be opened!\n");
    }
    else
	{ 
        fprintf(cfPtr,"%s ",txt);
    }

    fclose(cfPtr);


	return;
}

/* CreateStack: will initalize the stack to empty			*/
/*		preconditions: Stack variable declared				*/
/*		postconditions: Stack will be initialized and empty */
void CreateStack(Stack *s)
{	
	s->top=0;//' ' maybe
	return;
}

/* Push: puts an item onto the stack						*/
/*		pre: Stack exists and is not full	                */
/*		post: item will be put on the top of stack          */
void Push(StackEntry item,Stack *s)
{
	if(StackFull(s))
		puts("Error : A Push to a full stack has been attempted");
	else
	{	s->entry[s->top]= item;
		s->top =s->top +1;
	}
	return;
}




/* Pop: pops an item from the stack						      */
/*		pre: Stack exists and is not empty	                  */
/*		post: Top of stack has been removed & stored in item  */
void Pop(StackEntry *item,Stack *s)
{
	if(StackEmpty(s))
		puts("Error : A pop from an empty stack has been attempted");
	else
	{	
		s->top=s->top-1;
		*item=s->entry[s->top];
	}
	return;
}

/* StackEmpty: tests if stack is empty or not				*/
/*		pre: Stack exists and is initialized                */
/*		post: returns TRUE (if empty) or FALSE(if nonempty) */
void isPalindrome(char strsrc[], char strtmp[])
{
	strcpy(strtmp,strsrc);
		
	if(strcmp(strsrc,strtmp)==0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

and PalindromeDefs.h

#include <stdio.h>
#ifndef MYSTACK
#define MYSTACK

#define MAXSTACKSIZE 10
typedef char StackEntry; //change to char
typedef enum {FALSE=0, TRUE=1} Boolean;
typedef struct stack
{	int top;
	StackEntry entry[MAXSTACKSIZE];
} Stack;   // Now we can declare variables of type Stack

/* CreateStack: will initalize the stack to empty			*/
/*		preconditions: Stack variable declared				*/
/*		postconditions: Stack will be initialized and empty */
void CreateStack(Stack *s);

/* Push: puts an item onto the stack						*/
/*		pre: Stack exists and is not full	                */
/*		post: item will be put on the top of stack          */
void Push(StackEntry item,Stack *s);

/* Pop: pops an item from the stack						      */
/*		pre: Stack exists and is not empty	                  */
/*		post: Top of stack has been removed & stored in item  */
void Pop(StackEntry *item,Stack *s);

/* StackEmpty: tests if stack is empty or not				*/
/*		pre: Stack exists and is initialized                */
/*		post: returns TRUE (if empty) or FALSE(if nonempty) */
Boolean StackEmpty(Stack *s);	 //pass by address here is for efficiency

/* StackFull: tests if stack is empty or not				*/
/*		pre: Stack exists and is initialized                */
/*		post: returns TRUE (if empty) or FALSE(if nonempty) */
Boolean StackEmpty(Stack *s);	

#endif

void readFile();
void writeFile(char txt[]);
void ifPalindrome();

i wish you'd use as your opening tag... the syntax coloring makes it readable, and the line numbers help debug discussions.

you still have about 20mins to edit :P[code=c] as your opening tag... the syntax coloring makes it readable, and the line numbers help debug discussions.

you still have about 20mins to edit :P

I've taken out lines 35-42 in palindromesdefs.c
that's the while statement

This article has been dead for over six months. Start a new discussion instead.