Well a few fixes - watch for the !! comments
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 120
struct charRecord
{
char letter;
struct charRecord *nextNode;
};
typedef struct charRecord charNode;
FILE* openFile(char *filename, char *mode);
void loadChar(char *filename, charNode **start);
void applyRules(charNode **start);
void printList ( charNode *start);
void freeList(charNode **start);
charNode* createNode(char nodeLetter);
int main(int argc, char *argv[])
{
charNode *start =NULL;
/* Check command line arguments */
if (argc != 2)
{
printf("\nSyntax : euro file\n");
return 0;
}
loadChar(argv[1], &start);
printList(start);
applyRules(&start);
printList(start);
freeList(&start);
return 0;
}
/*
Your program will get name of the file containning the english
to be converted as a command line argument
*/
FILE* openFile(char *filename, char *mode)
{
/* open a FILE stream to filename with given mode */
FILE *file = fopen(filename, mode);
if (file == NULL)
{
printf("ERROR - unable to read %s\n", filename);
exit(0);
}
return file;
}
/* it will tren read the file and build up the English message,
character by character in a SINGLE linked list.
Each Node in the list will contain a single chracter.
*/
void loadChar(char *filename, charNode **start)
{
int c;/*!! needs to be an int, to be able to store all chars AND EOF */
/* Pointer to the file. FILE is a structure defined in <stdio.h>*/
charNode *temp, *tail = *start;
/* Open the file - no error checking done */
FILE *fin = openFile(filename, "r");
/* Read one character at a time, checking */
while ((c = fgetc(fin)) != EOF)
{
temp = createNode(c) ;
if(tail == NULL)
{
*start = tail = temp ;
}
else
{
tail->nextNode = temp;
tail = temp;
}
}
if (!feof(fin))
{
printf("ERROR - unable to read lines in %s\n", filename);
exit(0);
}
fclose(fin);
}
/*
you will need to apply the rules to the message contained in the link list.
*/
void applyRules(charNode **start)
{
charNode *temp;
temp = *start;
while (temp != NULL && temp->nextNode != NULL )
{
if(temp->letter== 'w')
{
temp->letter ='v';
}
else if(temp->letter== temp->nextNode->letter)
{
temp->nextNode->letter ='\0'; /*!! not NULL */
}
else if(temp->letter== 'p' && temp->nextNode->letter == 'h')
{
temp->letter ='f';
temp->nextNode->letter ='\0'; /*!! not NULL */
}
else if(temp->letter== 't' && temp->nextNode->letter == 'h')
{
temp->letter ='z';
temp->nextNode->letter ='\0'; /*!! not NULL */
}
else if(temp->letter== 'o' && temp->nextNode->letter == 'u')
{
temp->letter ='u';
temp->nextNode->letter ='\0'; /*!! not NULL */
}
else if(temp->letter== 'e' && temp->nextNode->letter == 'a')
{
temp->letter ='e';
temp->nextNode->letter = '\0'; /*!! not NULL */
}
else if(temp->letter== 'c')
{
if(temp->letter== 'c' && (temp->nextNode->letter == 'e'
|| temp->nextNode->letter == 'i'
|| temp->nextNode->letter == 'y'))
{
temp->letter ='s';
}
else
{
temp->letter ='k';
}
}
else if( temp->letter== 'e' && temp->nextNode->letter==' ')
{
temp->letter='\0'; /*!! not NULL */
}
else if(temp->letter== 'e' && temp->nextNode->letter == 'd'
&& temp->nextNode->nextNode->letter == ' ')
{
temp->letter = 'd';
temp->nextNode->letter = '\0'; /*!! not NULL */
}
temp = temp->nextNode;
}
}
/* the program should print out the the Euro english */
void printList ( charNode *start)
{
/*
print out the contents of the linked list
start with the first item added
*/
while (start != NULL)
{
/*!! check for deleted characters */
if ( start->letter != '\0' ) printf("%c", start->letter);
start = start->nextNode;
}
printf("\n");
}
/* the programm should free all the memory used in the linked list */
void freeList(charNode **start)
{
/* free up all word records in linked list */
charNode *temp;
while (*start != NULL)
{
temp = *start;
*start = temp->nextNode;
free(temp);
}
}
/* the program allocate memory for the linked list */
charNode* createNode(char nodeLetter)
{
/* dynamicaly allocate memory for an charNode
make sure it is initialised correctly
*/
charNode *temp= /*!! no casting malloc in C - see FAQ */malloc(sizeof(charNode));
if (temp == NULL)
{
printf("WARNING - Memory allocation error\n");
exit(EXIT_FAILURE);
}
temp->letter = nodeLetter;
temp->nextNode = NULL;
return temp;
}
Example output
./a.out results.txt
#include <stdio.h>
int main ( int argc, char *argv[] ) {
char buff[BUFSIZ];
while ( fgets ( buff, BUFSIZ, stdin ) != NULL ) {
fputs ( buff, stdout );
}
return 0;
}
#inklud <stdio.h>
int main ( int argk, khar *argv[] ) {
khar buf[BUFSIZ];
vhil ( fgets ( buf, BUFSIZ, stdin ) != NUL ) {
fputs ( buf, stdut );
}
return 0;
}