SlideShare a Scribd company logo
1 of 10
Download to read offline
void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);
When this function is called, it should start searching from the current cursor position, looking
for the next occurrence of the character oldch in the rest of the buffer.
If it finds it, simply replace oldch with newch. Search should leave the cursor after the last
character replaced in the buffer.
If oldch does not occur between the cursor and the end of the buffer, then there is nothing to
replace and thus search should leave the cursor unchanged (in the original place)
int SearchStrBuffer(bufferADT buffer, char* str);
When this function is called, it should start searching from the current cursor position, looking
for the next occurrence of the string str in the rest of the buffer.
If it finds it, search should leave the cursor after the found string and return the value TRUE or 1
If the string str does not occur between the cursor and the end of the buffer, then search should
leave the cursor unchanged and return FALSE or 0
First, you need to add the above prototypes into buffer.h.
Second, you need to implement them in listbuf.c
Third, you need to update editor.c so that you can call those functions. So include the following
in editor.c:
o If user enters: Rxy, your editor.c should call ReplaceCharInBuffer (buffer, 'x', 'y') to replace
all the occurrences of character 'x' with character 'y' after the cursor.
o If user enters: Sxyz, your editor.c should call SearchStrBuffer (buffer, "xyz") to search string
"xyz" in the buffer after the cursor.
Solution
/*
* File: editor.c
* --------------
* This program implements a simple character editor, which
* is used to test the buffer abstraction. The editor reads
* and executes simple commands entered by the user.
*/
#include
#include
#include "genlib.h"
#include "buffer.h"
#include "simpio.h"
#include "string.h"
/* Private function prototypes */
static void ExecuteCommand(bufferADT buffer, string line);
static void HelpCommand(void);
/* Main program */
main()
{
bufferADT buffer;
buffer = NewBuffer();
while (TRUE) {
printf("*");
ExecuteCommand(buffer, GetLine());
DisplayBuffer(buffer);
}
FreeBuffer(buffer);
}
/*
* Function: ExecuteCommand
* Usage: ExecuteCommand(buffer, line);
* ------------------------------------
* This function parses the user command in the string line
* and executes it on the buffer.
*/
static void ExecuteCommand(bufferADT buffer, string line)
{
int i;
switch (toupper(line[0])) {
case 'R':
if(strlen(line) >= 3) {
ReplaceCharInBuffer(buffer, line[1], line[2]);
} else {
printf("Illegal command ");
}
break;
case 'S':
SearchStrBuffer(buffer, &line[1]);
break;
case 'I': for (i = 1; line[i] != '0'; i++) {
InsertCharacter(buffer, line[i]);
}
break;
case 'D': DeleteCharacter(buffer); break;
case 'F': MoveCursorForward(buffer); break;
case 'B': MoveCursorBackward(buffer); break;
case 'J': MoveCursorToStart(buffer); break;
case 'E': MoveCursorToEnd(buffer); break;
case 'H': HelpCommand(); break;
case 'Q': exit(0);
default: printf("Illegal command "); break;
}
}
/*
* Function: HelpCommand
* Usage: HelpCommand();
* ---------------------
* This function lists the available editor commands.
*/
static void HelpCommand(void)
{
printf("Use the following commands to edit the buffer: ");
printf(" I... Inserts text up to the end of the line ");
printf(" F Moves forward a character ");
printf(" B Moves backward a character ");
printf(" J Jumps to the beginning of the buffer ");
printf(" E Jumps to the end of the buffer ");
printf(" D Deletes the next character ");
printf(" R Replaces the specified character with the new character ");
printf(" S Searches to find the text up to the end of line ");
printf(" H Generates a help message ");
printf(" Q Quits the program ");
}
buffer.h
/*
* File: buffer.h
* --------------
* This file defines the interface for an editor buffer abstraction.
*/
#ifndef _buffer_h
#define _buffer_h
#include "genlib.h"
/*
* Type: bufferADT
* ---------------
* This type defines the abstract type used to represent
* an editor buffer.
*/
typedef struct bufferCDT *bufferADT;
/* Exported entries */
/*
* Function: NewBuffer
* Usage: buffer = NewBuffer();
* ----------------------------
* This function dynamically allocates enough memory for the
* underlying representation of a bufferADT and initializes
* it to represent an empty buffer.
*/
bufferADT NewBuffer(void);
/*
* Function: FreeBuffer
* Usage: FreeBuffer(buffer);
* --------------------------
* This function frees the storage associated with the buffer.
*/
void FreeBuffer(bufferADT buffer);
/*
* Functions: MoveCursorForward, MoveCursorBackward
* Usage: MoveCursorForward(buffer);
* MoveCursorBackward(buffer);
* ----------------------------------
* These functions move the cursor forward or backward one
* character, respectively. If you call MoveCursorForward
* at the end of the buffer or MoveCursorBackward at the
* beginning, the function call has no effect.
*/
void MoveCursorForward(bufferADT buffer);
void MoveCursorBackward(bufferADT buffer);
/*
* Functions: MoveCursorToStart, MoveCursorToEnd
* Usage: MoveCursorToStart(buffer);
* MoveCursorToEnd(buffer);
* -------------------------------
* These functions move the cursor to the start or the
* end of the buffer, respectively.
*/
void MoveCursorToStart(bufferADT buffer);
void MoveCursorToEnd(bufferADT buffer);
/*
* Function: InsertCharacter
* Usage: InsertCharacter(buffer, ch);
* -----------------------------------
* This function inserts the single character ch into the
* buffer at the current cursor position. The cursor is
* positioned after the inserted character, which allows
* for consecutive insertions.
*/
void InsertCharacter(bufferADT buffer, char ch);
/*
* Function: DeleteCharacter
* Usage: DeleteCharacter(buffer);
* -------------------------------
* This function deletes the character immediately after
* the cursor. If the cursor is at the end of the buffer,
* this function has no effect.
*/
void DeleteCharacter(bufferADT buffer);
/*
* Function: DisplayBuffer
* Usage: DisplayBuffer(buffer);
* -----------------------------
* This function displays the current contents of the buffer
* on the console.
*/
void DisplayBuffer(bufferADT buffer);
/*
* Function: ReplaceCharInBuffer
* Usage: ReplaceCharInBuffer(buffer, oldch, newch);
* -----------------------------
*/
void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);
/*
* Function: SearchStrBuffer
* Usage: SearchStrBuffer(buffer, str);
* -----------------------------
*/
int SearchStrBuffer(bufferADT buffer, char* str);
#endif
listbuf.c
/*
* File: listbuf.c
* ---------------
* This file implements the buffer.h abstraction using a linked
* list to represent the buffer.
*/
#include
#include "genlib.h"
#include "strlib.h"
#include "buffer.h"
/* Types */
typedef struct cellT {
char ch;
struct cellT *link;
} cellT;
struct bufferCDT {
cellT *start;
cellT *cursor;
};
/*
* Implementation notes: NewBuffer
* -------------------------------
* This function allocates an empty editor buffer, represented
* as a linked list. To simplify the link list operation, this
* implementation adopts the useful programming tactic of
* keeping an extra "dummy" cell at the beginning of each list,
* so that the empty buffer has the following representation:
*
* +-------+ +------+
* | o---+-----====>| |
* +-------+ / +------+
* | o---+---/ | NULL |
* +-------+ +------+
*/
bufferADT NewBuffer(void)
{
bufferADT buffer;
buffer = New(bufferADT);
buffer->start = buffer->cursor = New(cellT *);
buffer->start->link = NULL;
return (buffer);
}
/*
* Implementation notes: FreeBuffer
* --------------------------------
* FreeBuffer must free every cell in the buffer as well as
* the buffer storage itself. Note that the loop structure
* is not exactly the standard idiom for processing every
* cell within a linked list, because it is not legal to
* free a cell and later look at its link field. To avoid
* selecting fields in the structure after it has been freed,
* you have to copy the link pointer before calling FreeBlock.
*/
void FreeBuffer(bufferADT buffer)
{
cellT *cp, *next;
cp = buffer->start;
while (cp != NULL) {
next = cp->link;
FreeBlock(cp);
cp = next;
}
FreeBlock(buffer);
}
void MoveCursorForward(bufferADT buffer)
{
if (buffer->cursor->link != NULL) {
buffer->cursor = buffer->cursor->link;
}
}
void MoveCursorBackward(bufferADT buffer)
{
cellT *cp;
if (buffer->cursor != buffer->start) {
cp = buffer->start;
while (cp->link != buffer->cursor) {
cp = cp->link;
}
buffer->cursor = cp;
}
}
void MoveCursorToStart(bufferADT buffer)
{
buffer->cursor = buffer->start;
}
void MoveCursorToEnd(bufferADT buffer)
{
while (buffer->cursor->link != NULL) {
MoveCursorForward(buffer);
}
}
void InsertCharacter(bufferADT buffer, char ch)
{
cellT *cp;
cp = New(cellT *);
cp->ch = ch;
cp->link = buffer->cursor->link;
buffer->cursor->link = cp;
buffer->cursor = cp;
}
void DeleteCharacter(bufferADT buffer)
{
cellT *cp;
if (buffer->cursor->link != NULL) {
cp = buffer->cursor->link;
buffer->cursor->link = cp->link;
FreeBlock(cp);
}
}
void DisplayBuffer(bufferADT buffer)
{
cellT *cp;
for (cp = buffer->start->link; cp != NULL; cp = cp->link) {
printf(" %c", cp->ch);
}
printf(" ");
for (cp = buffer->start; cp != buffer->cursor; cp = cp->link) {
printf(" ");
}
printf("^ ");
}
void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch) {
cellT *cp;
for(cp = buffer->cursor; cp != NULL; cp = cp->link) {
if(cp->ch == oldch) {
cp->ch = newch;
buffer->cursor = cp;
return;
}
}
}
int SearchStrBuffer(bufferADT buffer, char *str) {
cellT *cp, *tmp;
int i;
for(cp = buffer->cursor; cp != NULL; cp = cp->link) {
tmp = cp;
i = 0;
while(tmp) {
if(tmp->ch == str[i++]) {
if(str[i] == '0') {
buffer->cursor = tmp;
return TRUE;
} else {
tmp = tmp->link;
}
} else {
break; // No point in searching further.
}
}
}
// If this point is reached, then the string was not found.
return FALSE;
}

More Related Content

Similar to void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);.pdf

Writing a TSDB from scratch_ performance optimizations.pdf
Writing a TSDB from scratch_ performance optimizations.pdfWriting a TSDB from scratch_ performance optimizations.pdf
Writing a TSDB from scratch_ performance optimizations.pdfRomanKhavronenko
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxadampcarr67227
 
all i need is these two filesCreate VectorContainer.hppCreat.docx
all i need is these two filesCreate VectorContainer.hppCreat.docxall i need is these two filesCreate VectorContainer.hppCreat.docx
all i need is these two filesCreate VectorContainer.hppCreat.docxjack60216
 
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdfUsing c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdffashiongallery1
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2PVS-Studio
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstackThierry Gayet
 
Elements of C++11
Elements of C++11Elements of C++11
Elements of C++11Uilian Ries
 
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdfC++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdfrohit219406
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxbradburgess22840
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfjyothimuppasani1
 
Radix 2 code
Radix 2 codeRadix 2 code
Radix 2 codepradipakv
 
What is storage class
What is storage classWhat is storage class
What is storage classIsha Aggarwal
 

Similar to void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);.pdf (20)

Writing a TSDB from scratch_ performance optimizations.pdf
Writing a TSDB from scratch_ performance optimizations.pdfWriting a TSDB from scratch_ performance optimizations.pdf
Writing a TSDB from scratch_ performance optimizations.pdf
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
Buffer overflow attack
Buffer overflow attackBuffer overflow attack
Buffer overflow attack
 
Introduction to c part -3
Introduction to c   part -3Introduction to c   part -3
Introduction to c part -3
 
NodeJs Modules.pdf
NodeJs Modules.pdfNodeJs Modules.pdf
NodeJs Modules.pdf
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docx
 
all i need is these two filesCreate VectorContainer.hppCreat.docx
all i need is these two filesCreate VectorContainer.hppCreat.docxall i need is these two filesCreate VectorContainer.hppCreat.docx
all i need is these two filesCreate VectorContainer.hppCreat.docx
 
NIO and NIO2
NIO and NIO2NIO and NIO2
NIO and NIO2
 
Nio nio2
Nio nio2Nio nio2
Nio nio2
 
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdfUsing c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
 
Elements of C++11
Elements of C++11Elements of C++11
Elements of C++11
 
Stack & heap
Stack & heap Stack & heap
Stack & heap
 
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdfC++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdf
 
Radix 2 code
Radix 2 codeRadix 2 code
Radix 2 code
 
Memory Manglement in Raku
Memory Manglement in RakuMemory Manglement in Raku
Memory Manglement in Raku
 
What is storage class
What is storage classWhat is storage class
What is storage class
 

More from arihantmobilepoint15

In a classic study of blood types in Italy, Dr. Luigi Cavalli sfo.pdf
In a classic study of blood types in Italy, Dr. Luigi Cavalli sfo.pdfIn a classic study of blood types in Italy, Dr. Luigi Cavalli sfo.pdf
In a classic study of blood types in Italy, Dr. Luigi Cavalli sfo.pdfarihantmobilepoint15
 
I need help with questions 7 and 8 I think what I circled is correct.pdf
I need help with questions 7 and 8 I think what I circled is correct.pdfI need help with questions 7 and 8 I think what I circled is correct.pdf
I need help with questions 7 and 8 I think what I circled is correct.pdfarihantmobilepoint15
 
How to write main program for converting infix to postfix that allow.pdf
How to write main program for converting infix to postfix that allow.pdfHow to write main program for converting infix to postfix that allow.pdf
How to write main program for converting infix to postfix that allow.pdfarihantmobilepoint15
 
How can I upload a picture in here I tried with image properties ic.pdf
How can I upload a picture in here I tried with image properties ic.pdfHow can I upload a picture in here I tried with image properties ic.pdf
How can I upload a picture in here I tried with image properties ic.pdfarihantmobilepoint15
 
For some DNAs it is possible to separate the two strands, after dena.pdf
For some DNAs it is possible to separate the two strands, after dena.pdfFor some DNAs it is possible to separate the two strands, after dena.pdf
For some DNAs it is possible to separate the two strands, after dena.pdfarihantmobilepoint15
 
Explain how stem cells obtained from IVF leftovers and somatic cell .pdf
Explain how stem cells obtained from IVF leftovers and somatic cell .pdfExplain how stem cells obtained from IVF leftovers and somatic cell .pdf
Explain how stem cells obtained from IVF leftovers and somatic cell .pdfarihantmobilepoint15
 
Does it appear that there is a difference in mean population size....pdf
Does it appear that there is a difference in mean population size....pdfDoes it appear that there is a difference in mean population size....pdf
Does it appear that there is a difference in mean population size....pdfarihantmobilepoint15
 
Discussion Post The Constitution 19 19 Think about the discussion in.pdf
Discussion Post The Constitution 19 19 Think about the discussion in.pdfDiscussion Post The Constitution 19 19 Think about the discussion in.pdf
Discussion Post The Constitution 19 19 Think about the discussion in.pdfarihantmobilepoint15
 
Disease data for two independent groups are summarized into 2 by 2 t.pdf
Disease data for two independent groups are summarized into 2 by 2 t.pdfDisease data for two independent groups are summarized into 2 by 2 t.pdf
Disease data for two independent groups are summarized into 2 by 2 t.pdfarihantmobilepoint15
 
Darwinian evolution, aka Darwinism, was merged with population g.pdf
Darwinian evolution, aka Darwinism, was merged with population g.pdfDarwinian evolution, aka Darwinism, was merged with population g.pdf
Darwinian evolution, aka Darwinism, was merged with population g.pdfarihantmobilepoint15
 
Determine the theoretical probability of flipping a TAIL on a coin. .pdf
Determine the theoretical probability of flipping a TAIL on a coin. .pdfDetermine the theoretical probability of flipping a TAIL on a coin. .pdf
Determine the theoretical probability of flipping a TAIL on a coin. .pdfarihantmobilepoint15
 
Construct a java method.   This method chooses the number of sti.pdf
Construct a java method.    This method chooses the number of sti.pdfConstruct a java method.    This method chooses the number of sti.pdf
Construct a java method.   This method chooses the number of sti.pdfarihantmobilepoint15
 
Complete the sentences with the correct terms. Solution1. Phot.pdf
Complete the sentences with the correct terms.  Solution1. Phot.pdfComplete the sentences with the correct terms.  Solution1. Phot.pdf
Complete the sentences with the correct terms. Solution1. Phot.pdfarihantmobilepoint15
 
BIOCHEMISTRY 1. explain carbohydrate nonmenclature (aldose vs. ket.pdf
BIOCHEMISTRY 1. explain carbohydrate nonmenclature (aldose vs. ket.pdfBIOCHEMISTRY 1. explain carbohydrate nonmenclature (aldose vs. ket.pdf
BIOCHEMISTRY 1. explain carbohydrate nonmenclature (aldose vs. ket.pdfarihantmobilepoint15
 
Can a degree 3 polynomial intersect a degree 4 polynomial in exactly .pdf
Can a degree 3 polynomial intersect a degree 4 polynomial in exactly .pdfCan a degree 3 polynomial intersect a degree 4 polynomial in exactly .pdf
Can a degree 3 polynomial intersect a degree 4 polynomial in exactly .pdfarihantmobilepoint15
 
All chordates have while all vertebrates have homeothermya notochor.pdf
All chordates have while all vertebrates have  homeothermya notochor.pdfAll chordates have while all vertebrates have  homeothermya notochor.pdf
All chordates have while all vertebrates have homeothermya notochor.pdfarihantmobilepoint15
 
6. What factor describes the overall thermal performance of a buildin.pdf
6. What factor describes the overall thermal performance of a buildin.pdf6. What factor describes the overall thermal performance of a buildin.pdf
6. What factor describes the overall thermal performance of a buildin.pdfarihantmobilepoint15
 
8) IntheSpapcelabLifeSciencePayload,14maleratsweresenttospace.Uponth.pdf
8) IntheSpapcelabLifeSciencePayload,14maleratsweresenttospace.Uponth.pdf8) IntheSpapcelabLifeSciencePayload,14maleratsweresenttospace.Uponth.pdf
8) IntheSpapcelabLifeSciencePayload,14maleratsweresenttospace.Uponth.pdfarihantmobilepoint15
 
After reading about the history of welfare in the USA, describe in y.pdf
After reading about the history of welfare in the USA, describe in y.pdfAfter reading about the history of welfare in the USA, describe in y.pdf
After reading about the history of welfare in the USA, describe in y.pdfarihantmobilepoint15
 
You are examining a copolymer for its potential as a material for .pdf
You are examining a copolymer for its potential as a material for .pdfYou are examining a copolymer for its potential as a material for .pdf
You are examining a copolymer for its potential as a material for .pdfarihantmobilepoint15
 

More from arihantmobilepoint15 (20)

In a classic study of blood types in Italy, Dr. Luigi Cavalli sfo.pdf
In a classic study of blood types in Italy, Dr. Luigi Cavalli sfo.pdfIn a classic study of blood types in Italy, Dr. Luigi Cavalli sfo.pdf
In a classic study of blood types in Italy, Dr. Luigi Cavalli sfo.pdf
 
I need help with questions 7 and 8 I think what I circled is correct.pdf
I need help with questions 7 and 8 I think what I circled is correct.pdfI need help with questions 7 and 8 I think what I circled is correct.pdf
I need help with questions 7 and 8 I think what I circled is correct.pdf
 
How to write main program for converting infix to postfix that allow.pdf
How to write main program for converting infix to postfix that allow.pdfHow to write main program for converting infix to postfix that allow.pdf
How to write main program for converting infix to postfix that allow.pdf
 
How can I upload a picture in here I tried with image properties ic.pdf
How can I upload a picture in here I tried with image properties ic.pdfHow can I upload a picture in here I tried with image properties ic.pdf
How can I upload a picture in here I tried with image properties ic.pdf
 
For some DNAs it is possible to separate the two strands, after dena.pdf
For some DNAs it is possible to separate the two strands, after dena.pdfFor some DNAs it is possible to separate the two strands, after dena.pdf
For some DNAs it is possible to separate the two strands, after dena.pdf
 
Explain how stem cells obtained from IVF leftovers and somatic cell .pdf
Explain how stem cells obtained from IVF leftovers and somatic cell .pdfExplain how stem cells obtained from IVF leftovers and somatic cell .pdf
Explain how stem cells obtained from IVF leftovers and somatic cell .pdf
 
Does it appear that there is a difference in mean population size....pdf
Does it appear that there is a difference in mean population size....pdfDoes it appear that there is a difference in mean population size....pdf
Does it appear that there is a difference in mean population size....pdf
 
Discussion Post The Constitution 19 19 Think about the discussion in.pdf
Discussion Post The Constitution 19 19 Think about the discussion in.pdfDiscussion Post The Constitution 19 19 Think about the discussion in.pdf
Discussion Post The Constitution 19 19 Think about the discussion in.pdf
 
Disease data for two independent groups are summarized into 2 by 2 t.pdf
Disease data for two independent groups are summarized into 2 by 2 t.pdfDisease data for two independent groups are summarized into 2 by 2 t.pdf
Disease data for two independent groups are summarized into 2 by 2 t.pdf
 
Darwinian evolution, aka Darwinism, was merged with population g.pdf
Darwinian evolution, aka Darwinism, was merged with population g.pdfDarwinian evolution, aka Darwinism, was merged with population g.pdf
Darwinian evolution, aka Darwinism, was merged with population g.pdf
 
Determine the theoretical probability of flipping a TAIL on a coin. .pdf
Determine the theoretical probability of flipping a TAIL on a coin. .pdfDetermine the theoretical probability of flipping a TAIL on a coin. .pdf
Determine the theoretical probability of flipping a TAIL on a coin. .pdf
 
Construct a java method.   This method chooses the number of sti.pdf
Construct a java method.    This method chooses the number of sti.pdfConstruct a java method.    This method chooses the number of sti.pdf
Construct a java method.   This method chooses the number of sti.pdf
 
Complete the sentences with the correct terms. Solution1. Phot.pdf
Complete the sentences with the correct terms.  Solution1. Phot.pdfComplete the sentences with the correct terms.  Solution1. Phot.pdf
Complete the sentences with the correct terms. Solution1. Phot.pdf
 
BIOCHEMISTRY 1. explain carbohydrate nonmenclature (aldose vs. ket.pdf
BIOCHEMISTRY 1. explain carbohydrate nonmenclature (aldose vs. ket.pdfBIOCHEMISTRY 1. explain carbohydrate nonmenclature (aldose vs. ket.pdf
BIOCHEMISTRY 1. explain carbohydrate nonmenclature (aldose vs. ket.pdf
 
Can a degree 3 polynomial intersect a degree 4 polynomial in exactly .pdf
Can a degree 3 polynomial intersect a degree 4 polynomial in exactly .pdfCan a degree 3 polynomial intersect a degree 4 polynomial in exactly .pdf
Can a degree 3 polynomial intersect a degree 4 polynomial in exactly .pdf
 
All chordates have while all vertebrates have homeothermya notochor.pdf
All chordates have while all vertebrates have  homeothermya notochor.pdfAll chordates have while all vertebrates have  homeothermya notochor.pdf
All chordates have while all vertebrates have homeothermya notochor.pdf
 
6. What factor describes the overall thermal performance of a buildin.pdf
6. What factor describes the overall thermal performance of a buildin.pdf6. What factor describes the overall thermal performance of a buildin.pdf
6. What factor describes the overall thermal performance of a buildin.pdf
 
8) IntheSpapcelabLifeSciencePayload,14maleratsweresenttospace.Uponth.pdf
8) IntheSpapcelabLifeSciencePayload,14maleratsweresenttospace.Uponth.pdf8) IntheSpapcelabLifeSciencePayload,14maleratsweresenttospace.Uponth.pdf
8) IntheSpapcelabLifeSciencePayload,14maleratsweresenttospace.Uponth.pdf
 
After reading about the history of welfare in the USA, describe in y.pdf
After reading about the history of welfare in the USA, describe in y.pdfAfter reading about the history of welfare in the USA, describe in y.pdf
After reading about the history of welfare in the USA, describe in y.pdf
 
You are examining a copolymer for its potential as a material for .pdf
You are examining a copolymer for its potential as a material for .pdfYou are examining a copolymer for its potential as a material for .pdf
You are examining a copolymer for its potential as a material for .pdf
 

Recently uploaded

Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxLimon Prince
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17Celine George
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxCeline George
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MysoreMuleSoftMeetup
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfJerry Chew
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesAmanpreetKaur157993
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 

Recently uploaded (20)

Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 

void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);.pdf

  • 1. void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch); When this function is called, it should start searching from the current cursor position, looking for the next occurrence of the character oldch in the rest of the buffer. If it finds it, simply replace oldch with newch. Search should leave the cursor after the last character replaced in the buffer. If oldch does not occur between the cursor and the end of the buffer, then there is nothing to replace and thus search should leave the cursor unchanged (in the original place) int SearchStrBuffer(bufferADT buffer, char* str); When this function is called, it should start searching from the current cursor position, looking for the next occurrence of the string str in the rest of the buffer. If it finds it, search should leave the cursor after the found string and return the value TRUE or 1 If the string str does not occur between the cursor and the end of the buffer, then search should leave the cursor unchanged and return FALSE or 0 First, you need to add the above prototypes into buffer.h. Second, you need to implement them in listbuf.c Third, you need to update editor.c so that you can call those functions. So include the following in editor.c: o If user enters: Rxy, your editor.c should call ReplaceCharInBuffer (buffer, 'x', 'y') to replace all the occurrences of character 'x' with character 'y' after the cursor. o If user enters: Sxyz, your editor.c should call SearchStrBuffer (buffer, "xyz") to search string "xyz" in the buffer after the cursor. Solution /* * File: editor.c * -------------- * This program implements a simple character editor, which * is used to test the buffer abstraction. The editor reads * and executes simple commands entered by the user. */ #include #include #include "genlib.h" #include "buffer.h"
  • 2. #include "simpio.h" #include "string.h" /* Private function prototypes */ static void ExecuteCommand(bufferADT buffer, string line); static void HelpCommand(void); /* Main program */ main() { bufferADT buffer; buffer = NewBuffer(); while (TRUE) { printf("*"); ExecuteCommand(buffer, GetLine()); DisplayBuffer(buffer); } FreeBuffer(buffer); } /* * Function: ExecuteCommand * Usage: ExecuteCommand(buffer, line); * ------------------------------------ * This function parses the user command in the string line * and executes it on the buffer. */ static void ExecuteCommand(bufferADT buffer, string line) { int i; switch (toupper(line[0])) { case 'R': if(strlen(line) >= 3) { ReplaceCharInBuffer(buffer, line[1], line[2]); } else { printf("Illegal command "); } break; case 'S':
  • 3. SearchStrBuffer(buffer, &line[1]); break; case 'I': for (i = 1; line[i] != '0'; i++) { InsertCharacter(buffer, line[i]); } break; case 'D': DeleteCharacter(buffer); break; case 'F': MoveCursorForward(buffer); break; case 'B': MoveCursorBackward(buffer); break; case 'J': MoveCursorToStart(buffer); break; case 'E': MoveCursorToEnd(buffer); break; case 'H': HelpCommand(); break; case 'Q': exit(0); default: printf("Illegal command "); break; } } /* * Function: HelpCommand * Usage: HelpCommand(); * --------------------- * This function lists the available editor commands. */ static void HelpCommand(void) { printf("Use the following commands to edit the buffer: "); printf(" I... Inserts text up to the end of the line "); printf(" F Moves forward a character "); printf(" B Moves backward a character "); printf(" J Jumps to the beginning of the buffer "); printf(" E Jumps to the end of the buffer "); printf(" D Deletes the next character "); printf(" R Replaces the specified character with the new character "); printf(" S Searches to find the text up to the end of line "); printf(" H Generates a help message "); printf(" Q Quits the program "); }
  • 4. buffer.h /* * File: buffer.h * -------------- * This file defines the interface for an editor buffer abstraction. */ #ifndef _buffer_h #define _buffer_h #include "genlib.h" /* * Type: bufferADT * --------------- * This type defines the abstract type used to represent * an editor buffer. */ typedef struct bufferCDT *bufferADT; /* Exported entries */ /* * Function: NewBuffer * Usage: buffer = NewBuffer(); * ---------------------------- * This function dynamically allocates enough memory for the * underlying representation of a bufferADT and initializes * it to represent an empty buffer. */ bufferADT NewBuffer(void); /* * Function: FreeBuffer * Usage: FreeBuffer(buffer); * -------------------------- * This function frees the storage associated with the buffer. */ void FreeBuffer(bufferADT buffer); /* * Functions: MoveCursorForward, MoveCursorBackward
  • 5. * Usage: MoveCursorForward(buffer); * MoveCursorBackward(buffer); * ---------------------------------- * These functions move the cursor forward or backward one * character, respectively. If you call MoveCursorForward * at the end of the buffer or MoveCursorBackward at the * beginning, the function call has no effect. */ void MoveCursorForward(bufferADT buffer); void MoveCursorBackward(bufferADT buffer); /* * Functions: MoveCursorToStart, MoveCursorToEnd * Usage: MoveCursorToStart(buffer); * MoveCursorToEnd(buffer); * ------------------------------- * These functions move the cursor to the start or the * end of the buffer, respectively. */ void MoveCursorToStart(bufferADT buffer); void MoveCursorToEnd(bufferADT buffer); /* * Function: InsertCharacter * Usage: InsertCharacter(buffer, ch); * ----------------------------------- * This function inserts the single character ch into the * buffer at the current cursor position. The cursor is * positioned after the inserted character, which allows * for consecutive insertions. */ void InsertCharacter(bufferADT buffer, char ch); /* * Function: DeleteCharacter * Usage: DeleteCharacter(buffer); * ------------------------------- * This function deletes the character immediately after * the cursor. If the cursor is at the end of the buffer,
  • 6. * this function has no effect. */ void DeleteCharacter(bufferADT buffer); /* * Function: DisplayBuffer * Usage: DisplayBuffer(buffer); * ----------------------------- * This function displays the current contents of the buffer * on the console. */ void DisplayBuffer(bufferADT buffer); /* * Function: ReplaceCharInBuffer * Usage: ReplaceCharInBuffer(buffer, oldch, newch); * ----------------------------- */ void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch); /* * Function: SearchStrBuffer * Usage: SearchStrBuffer(buffer, str); * ----------------------------- */ int SearchStrBuffer(bufferADT buffer, char* str); #endif listbuf.c /* * File: listbuf.c * --------------- * This file implements the buffer.h abstraction using a linked * list to represent the buffer. */ #include #include "genlib.h" #include "strlib.h"
  • 7. #include "buffer.h" /* Types */ typedef struct cellT { char ch; struct cellT *link; } cellT; struct bufferCDT { cellT *start; cellT *cursor; }; /* * Implementation notes: NewBuffer * ------------------------------- * This function allocates an empty editor buffer, represented * as a linked list. To simplify the link list operation, this * implementation adopts the useful programming tactic of * keeping an extra "dummy" cell at the beginning of each list, * so that the empty buffer has the following representation: * * +-------+ +------+ * | o---+-----====>| | * +-------+ / +------+ * | o---+---/ | NULL | * +-------+ +------+ */ bufferADT NewBuffer(void) { bufferADT buffer; buffer = New(bufferADT); buffer->start = buffer->cursor = New(cellT *); buffer->start->link = NULL; return (buffer); } /* * Implementation notes: FreeBuffer * --------------------------------
  • 8. * FreeBuffer must free every cell in the buffer as well as * the buffer storage itself. Note that the loop structure * is not exactly the standard idiom for processing every * cell within a linked list, because it is not legal to * free a cell and later look at its link field. To avoid * selecting fields in the structure after it has been freed, * you have to copy the link pointer before calling FreeBlock. */ void FreeBuffer(bufferADT buffer) { cellT *cp, *next; cp = buffer->start; while (cp != NULL) { next = cp->link; FreeBlock(cp); cp = next; } FreeBlock(buffer); } void MoveCursorForward(bufferADT buffer) { if (buffer->cursor->link != NULL) { buffer->cursor = buffer->cursor->link; } } void MoveCursorBackward(bufferADT buffer) { cellT *cp; if (buffer->cursor != buffer->start) { cp = buffer->start; while (cp->link != buffer->cursor) { cp = cp->link; } buffer->cursor = cp; } }
  • 9. void MoveCursorToStart(bufferADT buffer) { buffer->cursor = buffer->start; } void MoveCursorToEnd(bufferADT buffer) { while (buffer->cursor->link != NULL) { MoveCursorForward(buffer); } } void InsertCharacter(bufferADT buffer, char ch) { cellT *cp; cp = New(cellT *); cp->ch = ch; cp->link = buffer->cursor->link; buffer->cursor->link = cp; buffer->cursor = cp; } void DeleteCharacter(bufferADT buffer) { cellT *cp; if (buffer->cursor->link != NULL) { cp = buffer->cursor->link; buffer->cursor->link = cp->link; FreeBlock(cp); } } void DisplayBuffer(bufferADT buffer) { cellT *cp; for (cp = buffer->start->link; cp != NULL; cp = cp->link) { printf(" %c", cp->ch); } printf(" "); for (cp = buffer->start; cp != buffer->cursor; cp = cp->link) {
  • 10. printf(" "); } printf("^ "); } void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch) { cellT *cp; for(cp = buffer->cursor; cp != NULL; cp = cp->link) { if(cp->ch == oldch) { cp->ch = newch; buffer->cursor = cp; return; } } } int SearchStrBuffer(bufferADT buffer, char *str) { cellT *cp, *tmp; int i; for(cp = buffer->cursor; cp != NULL; cp = cp->link) { tmp = cp; i = 0; while(tmp) { if(tmp->ch == str[i++]) { if(str[i] == '0') { buffer->cursor = tmp; return TRUE; } else { tmp = tmp->link; } } else { break; // No point in searching further. } } } // If this point is reached, then the string was not found. return FALSE; }