342 - HTML Syntax Checking

All about problems in Volume 3. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

sakhassan
Experienced poster
Posts: 105
Joined: Sat Mar 11, 2006 9:42 am
Location: cse,DU

Post by sakhassan »

Whats wrong with ma code ..... when submit it with Cpp it says invalid memory reference and with C it says compiler error .... i found no error in my compiler 'TC' :( .....

What's the size of 'a' line ??
is it ok with gets??
does the input accept only UPPERCASES tag ???
if it does not accept mixed cases tag then whats the error ??

Code: Select all


#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define M 1000
#define N 20


//char stack[M][N];

//int top;

struct ListNode
{

	char item[N];
	struct ListNode *next;
};

ListNode *top;

void push(char s[N])
{
    //strcpy(stack[top],s);
    //top++;

	ListNode *temp = new ListNode;
	strcpy(temp->item,s);
	temp->next=top;
	top=temp;
}

char *pop()
{
	char res[N];
	//top--;
	//strcpy(res,stack[top]);
	//top--;
	strcpy(res,top->item);
	top = top->next;

	return res;
}

int isEmpty()
{

	return top==NULL;
}


int main()
{


   int cases;
   int n,line,idx;
   int i,error,tag;
   char str[M],store[N],word[N],check[N];
   char ch;


   top = NULL;

   cases=0;

   while(1)
   {

	scanf("%d",&n);
	if(n==0)
	 break;

	gets(str);

	error = 0;
	line = 0;
	idx = 0;
	strcpy(word,"");
	strcpy(check,"");
	tag = 0;

	top=NULL;
	//for(i=0;i<M;i++)
	  //memset(stack[i],'\0',sizeof(stack[i]));
	while(line<n)
	{

		gets(str);
		//tag = 0;

		for(i=0;i<strlen(str);i++)
		{
		    ch = str[i];
		    if( (ch!='>' && ch!='/') && tag)
		    {
			word[idx++]=ch;
		    }
		    else if(ch=='<' && !tag)
		    {
			tag=1;
		    }
		    else if(ch=='>' && tag==1)
		    {
			if(!idx || idx>10)
				error = 2;
			else
			{
				word[idx]='\0';
				push(word);
				idx=0;
			}
			tag = 0;
		    }
		    else if(ch=='/' && tag==1)
		    {
			 tag=2;
		    }
		    else if(ch=='>' && tag==2)
		    {


			word[idx]='\0';
			if(!isEmpty())
			{
				strcpy(store,pop());
				if(strcmp(store,word)!=0)
					error =  3;
			}
			else
				error = 4;

			idx=0;
			tag = 0;
		    }
		    //if(tag)
		     //error = 1;
		    if(error)
		     break;

		}

		if(tag) error = 1;
		if(error)
		 break;

		line++;
	}

	i = line+1 ;

	if(!isEmpty() && !error)
	{
		error = 3;
		strcpy(store,pop());
		line--;
	}


	if(error)
		while( i<n)
		{
		 gets(str);
		 i++;
		}

	printf("Test Case %d\n",cases+1);
	cases++;
	if(!error)
	 printf("OK\n");
	else if(error==1)
	{
	       printf("line %d: bad characters in tag name.\n",line+1);
	}
	else if(error==2)
	{
	       printf("line %d: too many/few characters in tag name.\n",line+1);

	}
	else if(error==3)
	{
		printf("line %d: expected </%s>.\n",line+1,store);
	}
	else if(error==4)
	{
		printf("line %d: no matching begin tag.\n",line+1);
	}
   }


   return 0;
}



Thanks in advanced
Time that gone is gone forever ...

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Read the description again.
Remember that there is no maximum line length limit
So, you can use getchar to take input. I used an array of size 4000000 and gets().

Hope it helps.
Ami ekhono shopno dekhi...
HomePage

Post Reply

Return to “Volume 3 (300-399)”