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
//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.
Get Answers For Free
Most questions answered within 1 hours.