0

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?

Baba_yaga
  • 3
  • 5
  • Please let us know what exactly an _"extremely large input"_ is. Anyway I suspect `char elem[max]; scanf("%s",elem);` here. if your input is a string longer than `max` you'll get into trouble. – Jabberwocky Jun 14 '21 at 13:32
  • Extremely large inputs in the since when the operations are around 10^5 or 10^6 – Baba_yaga Jun 14 '21 at 13:34
  • And also I was not able to increase max more than what I have in my code if I increase then I get error – Baba_yaga Jun 14 '21 at 13:34
  • Please [edit] the question and put all relevant information _there_. Also show us an example of an input that triggers the problem. Read this: [ask] and this: [mcve] – Jabberwocky Jun 14 '21 at 13:35
  • @Jabberwocky the input is very large I cannot paste it here – Baba_yaga Jun 14 '21 at 13:39
  • Run it with you debugger. The debugger will show you where exactly the segfault happens and then you can inspect the contents of variables etc. and find out what's going on. Or maybe you should just check if none of your array indexes overflows. – Jabberwocky Jun 14 '21 at 13:42
  • 1
    If you need to accept arrays that exceed your stack size, you'll want to use heap memory, i.e., use dynamic memory allocation with malloc & friends. – 9769953 Jun 14 '21 at 13:46

0 Answers0