Sunday 25 September 2011


                                                  SIMPLE TEXT EDITOR       

AIM:
To write a C program to implement the operations of the text editor.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the input operation.
Step 3: If the option is insert, insert a character or word then read the line number, index and input. Insert the input character or word in position.
Step 4: If the option is insert a line, get the line number and the line to be added. Insert the line in the given position.
Step 5: If the option is delete a character, then read the line number and index position. Delete a character in that index.
Step 6: If the option is delete a word, read the line number, starting index of the word, delete the character between index positions.
Step 7: If the option is delete a line, then read the line number, delete the line and decrement the total number of lines.
Step 8: Stop the program.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
char str[25][50],str1[25][50];
int no1,nll;
void main()
{
void insertion(char [],int,int,int,int);
void deletion(int,int,int,int);
void display();
int ln,in,in1,ch1,i,flag,j,l;
char ch,temp[10];
clrscr();
printf("\nEnter the lines of text:\n");
scanf("%d",&no1);
nll=no1;
for(i=1;i<=no1;i++)
{
scanf("%s",str[i]);
}
while(1)
{
printf("\nEnter the line numbers:");
scanf("%d",&ln);
printf("\n1.Inserting a Character\n");
printf("\n2.Inserting a Word\n");
printf("\n3.Inserting a Sentence\n");
printf("\n4.Deleting a Character\n");
printf("\n5.Deleting a Word\n");
printf("\n6.Deleting a Setence\n");
printf("\n7.Display\n");
printf("\n8.Exit\n");
printf("\nEnter your choice:");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("\nEnter the character to be inserted and index:");
scanf("%s%d",temp,&in);
l=strlen(temp);
insertion(temp,l,ln,in,0);
display();
break;
case 2:
printf("\nEnter the word to be inserted and index:");
scanf("%s%d",temp,&in);
l=strlen(temp);
insertion(temp,l,ln,in,0);
display();
break;
case 3:
printf("\nEnter the text:");
scanf("%s",temp);
l=strlen(temp);
flag=1;
insertion(temp,l,ln,1,1);
nll++;
display();
break;
case 4:
printf("\nEnter the index of the character to be deleted:");
scanf("%d",&in);
deletion(in,in,ln,0);
display();
break;
case 5:
printf("\nEnter the starting index and the ending index of the word to be deleted:");
scanf("%d%d",&in,&in1);
deletion(in,in1,ln,0);
display();
break;
case 6:
deletion(0,0,ln,1);
display();
nll--;
break;
case 7:
display();
break;
case 8:
exit(0);
}
}
}

void display()
{

int i,l;
//printf("\n\t%s\n",str[1]);
//printf("\n\thihi\n");
for(i=1;i<=nll;i++)
{
strcpy(str[i],str1[i]);
l=strlen(str[i]);
if(l>0)
{
printf("%s\n",str[i]);
}
}
}

void deletion(int sindex,int eindex,int lno,int flag)
{
int i,j=0,l,k,len;
for(i=1;i<=nll;i++)
strcpy(str1[l],"");
if(flag==0)
{
for(i=1;i<lno;i++)
{
strcpy(str1[i],str[i]);
}
while(j<sindex)
{
str1[lno][j]=str[lno][j];
j++;
}
l=strlen(str[lno]);
if((l-1)!=sindex)
{
k=sindex;
j=j+eindex-sindex+1;
while(j<1)
{
str1[lno][k]=str[lno][j];
j++;
k++;
}
str1[lno][k]='\0';
}
else
{
str1[lno][j]='\0';
}
for(i=lno+1;i<=nll;i++)
{
strcpy(str1[i],str[i]);
}
}
else
{
for(i=1;i<lno;i++)
{
strcpy(str1[i],str[i]);
}
for(i=lno+1;i<=nll;i++)
{
strcpy(str1[i-1],str[i]);
}
}
}

void insertion(char a[],int n, int lno,int index,int flag)
{
int i,j=0,l,k,len;
for(i=1;i<=nll;i++)
strcpy(str1[i],"");
if(flag==0)
{
//printf("%s",str[1]);
for(i=1;i<lno;i++)
{
strcpy(str1[i],str[i]);
//printf("%s ",str[i]);
//printf("%s %d",str1[i],&i);
}
l=strlen(str1[lno]);
while(j<=index)
{
str1[lno][j]=str[lno][j];
j++;
}
if((l-1)!=index)
{
j=index+1;
for(i=0;i<n;i++)
{
str1[lno][j]=a[i];
j++;
}
len=strlen(str[lno]);
j=index+n+1;
k=index+1;
while(k<len)
{
str1[lno][j]=str[lno][k];
k++;
j++;
}
}
else
{
j=index+1;
for(i=0;i<n;i++)
{
str1[lno][j]=a[i];
j++;
}
str1[lno][j]='\0';
}
for(i=lno+1;i<=nll;i++)
{
strcpy(str1[i],str[i]);
}
}
else
{
for(i=1;i<=lno;i++)
{
strcpy(str1[i],str[i]);
}
strcpy(str1[lno+1],a);

for(i=lno+1;i<=nll;i++)
{
strcpy(str1[i+1],str[i]);
}
}
}

OUTPUT:

Enter the lines of text:
2
magna
college

Enter the line numbers:1
1.Inserting a Character
2.Inserting a Word
3.Inserting a Sentence
4.Deleting a Character
5.Deleting a Word
6.Deleting a Setence
7.Display
8.Exit
Enter your choice:1
Enter the character to be inserted and index:c
4
magnac
college
Enter the line numbers:1



Result
Thus above program executed and output verified.

0 comments:

Post a Comment