My question is to coding for a text editor where the operations
* 1-append
2-delete k characters from last
3-print nth character
4-undo
*
Input format is
*
The first line contains an integer,Q, denoting the number of operations.
Each line i of the Q subsequent lines (where 0<=i<=Q) defines an operation to be performed.
Each operation starts with a single integer, t (where t is either 1 or 2 or 3 or 4), denoting a type of operation as defined in the Problem Statement above.
If the operation requires an argument, t is followed by its space-separated argument.
For example, if t=1 and W="abcd" , line i will be 1 abcd.
*
This is my code
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define max 10000
char stack[max],super[max][max];
int top=-1,btop=-1,supert=-1;
int bottom=1,qqq=0;
void push(char elem[])
{
//top++;
btop=top;
int i=0;
strcat(stack,elem);
strcpy(super[++supert],stack);
}
void pop(int k)
{
top=strlen(stack);
for(int i=0;i<k;i++)
{
top--;
}
stack[top]='\0';
/*int i=0;
if(top==-1)
{
strcpy(stack,"");
}
else
{
i=top;
while(i<btop)
{
i++;
stack[i]='\0';
}
}*/
strcpy(super[++supert],stack);
}
int main()
{
int tot,op;
scanf("%d",&tot);
int q=0;
for(int i=0;i<tot;i++)
{
scanf("%d",&op);
switch(op)
{
case 1:;
char elem[max];
scanf("%s",elem);
push(elem);
//printf("%s\n",stack);
break;
case 2:;
int num;
scanf("%d",&num);
pop(num);
//printf("%s\n",stack);
break;
case 3:;
int k;
qqq++;
scanf("%d",&k);
/*if(qqq%2==0)
{
printf("%c\n",stack[k-3]);
}
else
{
printf("%c\n",stack[k-1]);
}*/
printf("%c\n",stack[k-1]);
break;
case 4:;
supert=supert-1;
strcpy(stack,super[supert]);
q--;
//printf("%s\n",stack);
break;
}
q++;
}
return 0;
}
For extremely large inputs(operations in the order of 10^5 or 10^6) I am getting segmentation fault Also I could not increase the size of max because if I increase I am getting an error Why is it so?