Question

Could you write a c- program that reads a text file into a linked list of...

Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements

1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise

2. Replace "sh" with ph

This is the text to be manipulated:

Paragraph1

She told us to take the trash out.

Why did she do that?

I wish she would not do that

Paragraph 2

We came home very late despite the fact we had school the next day.

We had a lot of fun

I miss my friends.

In c programming language please

Homework Answers

Answer #1
//header file included for standard input output
#include<stdio.h>
//header file included for string operations
#include<string.h>
//header file included for dynamic memory manipulation
#include<stdlib.h>

//structure of node i.e. linked list is defined.
struct node{
    //data for storing char type data
    char data;
    //next pointer points node of same structure type 
    struct node *next;
};

//cFlag to check whether 'c' is followed by the characters 'e', 'i' or 'y'
//initially 0 indicates that 'c' is not followed by the characters 'e', 'i' or 'y'
int cFlag=0;

//pointer to linked list start and last are declared
struct node *start=NULL;
struct node *last=NULL;

//prevChar to store previous character
char prevChar;

//insert() to insert file data into linked list
void insert(char c){
    //temp of node type is dynamically created
    struct node *temp = (struct node *)malloc(sizeof(struct node));

    if(temp==NULL){
        printf("\nMemory Full.");
        return;
    }

    //temp's data is assigned with file's data
    temp->data = c;
    temp->next = NULL;

    //if start is NUll i.e. linked list is empty
    //then make this initial temp node as start and end
    if(start==NULL){
        start = temp;
        last = temp;
        prevChar = c;
        return;
    }

    //cFlag is checked
    if(cFlag==0){
        //if condition satisfied then cFlag will be set to 1
        if((prevChar == 'c') && (c=='e'||c=='i'||c=='y')){
            cFlag = 1;
        }
    }

    
    prevChar = c;

    //file's data are inserted in the linked list's end 
    //so that insertion takes O(1) time to insert data at end of linked list
    last->next = temp;
    last = temp;
}

//processParagraphs() to process the linked list data based on cFlag
void processParagraphs(){
    struct node *temp = start;
    struct node *prev = start;
    //Replace all 'c' with 's' if followed by the characters 'e', 'i' or 'y i.e cFlag=1;
    if(cFlag == 1){
        while(temp!=NULL){
            if(temp->data=='c'){
                temp->data = 's';
            }
            temp = temp->next;
        }
    }
    else{ //otherwise Replace "sh" with ph
        while(temp!=NULL){
            prev = temp;
            temp = temp->next;
            if(temp!=NULL){
                if(prev->data=='s'&&temp->data=='h'){
                    prev->data = 'p';
                }
            }
        }
    }
}

//writeOutput() to generate a output file 
void writeOutput(){
    //filePath where output file is to be stored
    char filePath[20];
    strcpy(filePath,"output.txt");

    //file is opened
    FILE *fileOut = fopen(filePath,"w");
    if(fileOut==NULL){
        printf("\nFile Not Opened Correctly.");
        return;
    }

    //if file is successfully opened
    struct node *temp = start;

    //inserts all data into output file
    while(temp!=NULL){
        fputc(temp->data,fileOut);
        temp = temp->next;
    }

    //closing opened file
    fclose(fileOut);
}

//main() , program execution starts from here
int main(){
    //input file path is set
    char filePath[20];
    strcpy(filePath,"input.txt");

    //input file is opened for reading
    FILE *fileIn = fopen(filePath,"r"); 

    if(fileIn==NULL){
        printf("\nFile Not Opened Correctly.");
        return 0;
    }

    //if file opened successfully

    //ch variable to store characters from file
    char ch;

    //loop iterates till end of file is not reached
    while ((ch = fgetc(fileIn)) != EOF)
    {       
        //insert() is called to insert data into linked list
            insert(ch);
    }

    //input file is closed
    fclose(fileIn);

    //processParagraphs() called to process the linked list based on cFlag
    processParagraphs();

    //writeOutput() called to write in a output file
    writeOutput();

    return 0;
}

Instructions:

1. Must have input.txt file within the same directory upon which the program is going to work.

2.output.txt file will be generated by the program after processing input.txt file.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling...
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling to check if the file exists before attempting to open it. Read and print each string to the console. Next modify each word such at the first letter in each word is uppercase and all other letters are lowercase. Display the modified word on the console. Creating a text file: Open up Notepad and type in the following words. Save the file as collectionOfWords.txt...
What role could the governance of ethics have played if it had been in existence in...
What role could the governance of ethics have played if it had been in existence in the organization? Assess the leadership of Enron from an ethical perspective. THE FALL OF ENRON: A STAKEHOLDER FAILURE Once upon a time, there was a gleaming headquarters office tower in Houston, with a giant tilted "£"' in front, slowly revolving in the Texas sun. The Enron Corporation, which once ranked among the top Fortune 500 companies, collapsed in 2001 under a mountain of debt...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
Gender Bias in the Executive Suite Worldwide The Grant Thornton International Business Report (IBR) has described...
Gender Bias in the Executive Suite Worldwide The Grant Thornton International Business Report (IBR) has described itself as "a quarterly survey of business leaders from across the globe … surveying 11,500 businesses in 40 economies across the globe on an annual basis." 1 According to the 2011 IBR, the Asia Pacific region had a higher percentage (27 percent) of female chief executive officers (CEOs) than Europe and North America. Japan is the only Asia Pacific region exception. The report further...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT