GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Jun-2005, 10:02
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough

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...
CPP / C++ / C Code:
#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;
}
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
search linked list itsmecathys C++ Forum 20 18-Apr-2005 02:34
adding to linked list from external file cghv C Programming Language 3 09-Mar-2005 14:36
help on linked lists any1????? nick4 C Programming Language 1 17-May-2004 10:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:52.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.