
02-Jun-2005, 10:02
|
|
Member
|
|
Join Date: Apr 2005
Posts: 199
|
|
|
Removing Characters from a "String" in a Linked List
@eccoflame: Post questions to the forum, not in PMs.
Quote:
|
Originally Posted by eccoflame
i wanna make function that
*If a word is more than 3 characters long and ends with an ‘e’ then this can be removed.
*Replace all double characters with a single instance of the character.
i already stuck and it due tomorrow so can u help me...
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct charFile
{
char letter;
struct charFile *next;
};
typedef struct charFile charNode;
FILE* openFile(char *filename, char *mode);
void getfiles(char *filefrom);
void addLetterToList(charNode **start, charNode **end, charNode *temp);
void printList(charNode *start);
void change2(charNode **start);
void change3(charNode **start, charNode *temp2);
void deleteLink(charNode *temp, charNode *temp2);
charNode* newNode(char letter);
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Syntax : euro file\n");
exit(0);
}
getfiles(argv[1]);
return 0;
}
void getfiles(char *filefrom)
{
FILE *fin;
char c;
charNode *start = NULL;
charNode *end, *temp, temp2;
fin = openFile(filefrom, "r");
while ((c = fgetc(fin)) != EOF)
{
temp = newNode(c);
addLetterToList(&start, &end, temp);
}
fclose(fin);
printList(start);
change2(&start);
change3(&start, &temp2);
printList(start);
}
FILE* openFile(char *filename, char *mode)
{
FILE *file = fopen(filename, mode);
if (file == NULL)
{
printf("ERROR - Unable to open file %s\n", filename);
exit(1);
}
return file;
}
void addLetterToList(charNode **start, charNode **end, charNode *temp)
{
if (*start == NULL)
{
*start = temp;
}
else
{
(*end)->next = temp;
}
*end = temp;
}
void change2(charNode **start)
{
charNode *temp = *start;
while (temp != NULL)
{
if (temp->letter == 'w')
{
temp->letter = 'v';
}
temp = temp->next;
}
}
void change3(charNode **start, charNode *temp2)
{
charNode *temp = *start;
int count=1;
while (temp->next != NULL)
{
temp2 = temp->next;
if ((*temp).letter == 'p' && temp2->letter == 'h')
{
(*temp).letter = 'f';
deleteLink(temp, temp2);
}
if((*temp).letter == 't' && temp2->letter == 'h')
{
(*temp).letter = 'z';
deleteLink(temp,temp2);
}
if((*temp).letter == 'o' && temp2->letter == 'u')
{
(*temp).letter = 'u';
deleteLink(temp,temp2);
}
if((*temp).letter == 'e' && temp2->letter == 'a')
{
(*temp).letter = 'e';
deleteLink(temp,temp2);
}
if((*temp).letter == 'e' && temp2->letter == 'd' && (isalnum((int)temp ->letter)))
{
(temp)->letter = 'd';
deleteLink(temp,temp2);
}
if(!isalnum((int)((temp)->letter)) && ((*temp).letter == 'e' && count > 3))
{
(temp)->letter = temp->letter;
// (temp)->temp2 = (temp)->temp2;
}
/* if((*temp)->next == temp2->next && (isalpha(int)(*temp)->next))
{
(*temp)->next =(temp)->next;
temp2=(*temp)->next;
}
*/
temp = temp->next;
}
}
void deleteLink(charNode *temp, charNode *temp2)
{
temp2 = temp->next;
(*temp).next = (*temp2).next;
temp = (*temp).next;
free(temp2);
}
void printList(charNode *start)
{
charNode *bookmark = start;
while (start != NULL)
{
printf("%c", start->letter);
start = start->next;
if (start == bookmark)
{break;}
}
printf("\n");
}
charNode* newNode(char letter)
{
charNode *temp;
temp = (charNode *) malloc(sizeof(charNode));
if (temp == NULL)
{
printf("WARNING - Memory allocation error\n");
exit(EXIT_FAILURE);
}
temp->letter = letter;
temp->next = NULL;
return temp;
}
|
|
|