C program not c Write a rudimentary text editor that allows
C program not c++
Write a rudimentary text editor that allows the user to edit 5 lines of text, where each line is no longer than 80 characters. The editor should allow the user to continue editing as long as desired and should print the current state of the edit buffer after each edit. The editor is a \'line editor\' in that the user must edit one line at a time. The user can specify which line should be edited by choosing a line number from a menu. The only two editing operations possible are to replace an entire line with a new line, or to replace a substring in the line with a new substring.
Use these functions only and edit them accordingly:
int replaceInString(char original[], char substring[], char replace[]);
int findSubString(char original[], char toFind[]);
void insertString(char original[], int start, int length, char toInsert[]);
Solution
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int findSubString(char original[], char toFind[]){
int i,j;
int l1 = strlen(original);
int l2 = strlen( toFind );
for(i = 0; i < l1; i++ ){
if( original[i] == toFind[0] ){
for( j = 1; j < l2; j++){
if( original[i+j] != toFind[j] ){ break; }
}
if( j == l2 ){ return i; }
}
}
return -1;
}
void insertString(char original[], int start, int length, char toInsert[]){
int till = strlen(toInsert );
if( length < till ){ till = length; }
int i = 0;
for(; i < till; i++ ){
original[i+ start] = toInsert[i];
}
}
int replaceInString(char original[], char substring[], char replace[]){
int at = findSubString( original, substring );
printf(\"%d\ \", at);
if( at == -1 ){ return -1; }
insertString( original, at, strlen(substring), replace );
return 1; //dont know why returning int here?
}
void printText( char text[5][80 ] ){
int i;
for(i = 0; i < 5; i++){
printf(\"%d: %s\ \", i+1, text[i] );
}
}
int main(){
char text[5][80];
char inputStr[80];
char substring[80];
int lineNo;
char opr;
int i,j;
for( i = 0; i < 5; i++){
for( j = 0; j < 80; j++){
text[i][j] = \'\\0\';
}
}
while(1){
printf(\"Enter the line number to edit 1-5, enter anything else to exit: \");
scanf(\"%d\",&lineNo);
if( lineNo < 1 || lineNo > 5 ){ break; }
lineNo--;
scanf(\"%c\",&opr);
while(1){
printf(\"Enter operation to execute: l for replacing entire line, s for replacing a substring: \");
scanf(\"%c\",&opr);
if( opr == \'l\'){
printf(\"Replace with: \");
scanf(\"%s\", inputStr );
int i = 0;
for(; i < 80; i++){ text[lineNo][i] = \'\\0\'; }
insertString( text[lineNo], 0, 80, inputStr );
printText( text );
break;
}
if( opr == \'s\' ){
printf(\"Replace which substring: \");
scanf(\"%s\", substring );
printf(\"Replace with: \");
scanf(\"%s\", inputStr);
replaceInString( text[lineNo], substring, inputStr );
printText( text );
break;
}
printf(\"Incorrect option.\ \");
scanf(\"%c\",&opr);
}
}
}

