Here's a proposition. I cleaned up the code to make it simpler
and reorganized a bit the error management code (it was not
clear); you should also be careful about the indentation.
The diagnostic at the end is uncomplete, I let you finish it - the
tag checking code should work however. Try to rewrite it for
yourself. I don't guarantee this is bugproof.
Notice that you should avoid declaring global variables that
are only used in one function (if this is the case simply declare
them as locals); in your code, only the arrays and the 2
associated counters have to be globals. At last, there was a
bug in the string comparison function I provided earlier.
Code:
#include <stdio.h>
#define B_OPEN '<'
#define B_CLOSE '>'
#define BRACKET_MAX 10
#define B_CHAR_MAX 7
typedef char Tag[B_CHAR_MAX];
Tag bracket[BRACKET_MAX]; //THE OPENING BRACKETS ARRAY
unsigned bracket_nr=0; // INTEGER VALUES TO COUNT THE BRACKET NUMBERS
Tag close[BRACKET_MAX]; //THE CLOSING BRACKETS ARRAY
unsigned close_nr=0; // INTEGER VALUES TO COUNT THE BRACKET NUMBERS
int push(char c,Tag* tags,unsigned* pCounter) {
char bracket_char=0;
if(*pCounter>=BRACKET_MAX)
return -7; // Too many tags.
for(bracket_char=0;c!=EOF && c!='\n' && c!=B_CLOSE && bracket_char<B_CHAR_MAX;bracket_char++, c=getchar()) {
// Record tag name into the tags array.
tags[*pCounter][bracket_char]=c;
};
if (bracket_char>=B_CHAR_MAX)
return -5; // Tag name too long.
tags[*pCounter][bracket_char]='\0';
++*pCounter;
if(c==EOF || c=='\n')
return -3; // End of input.
return 0;
};
void writetags(Tag* tags,unsigned count) {
unsigned u;
for(u=0;u<count;++u) printf("%u:%s\n",u,tags[u]);
return;
};
int equal(const char* s1,const char* s2) {
if(!s1 || !s2) return 0;
while(*s1 && *s1==*s2) ++s1,++s2;
return !*s1 && !*s2;
}
int main (void) {
printf("Please Enter Your HTML statement..\n");
char c=0;
while((c=getchar())!=EOF && c!='\n') {
if(c==B_OPEN) {
int diag=0;
c=getchar(); // Drop <
if (c=='/') {
c=getchar(); // Drop /
diag=push(c,close,&close_nr); // THIS TO SEPARATE OPENING FROM CLOSING ARRAYS
} else
diag=push(c,bracket,&bracket_nr);
switch(diag) {
case -3:
printf("Unexpected end of input.\n");
break;
case -5:
printf("Tag name too long.\n");
break;
case -7:
printf("Too many tags.\n");
break;
}
}
}
if(bracket_nr==0 && close_nr==0){
printf("No brackets.\n");
} else {
unsigned u;
printf("\n** You have written %d brackets, \n",bracket_nr+close_nr);
printf("\n\n%u Of Which is/Are Opening Tags; and it's/they are :\n",bracket_nr);
writetags(bracket,bracket_nr);
printf("\n\nWhile %u is/are Closing Tags; And It's/They are:\n", close_nr);
writetags(close,close_nr);
printf("\n");
for(u=0;u<bracket_nr;++u) {
if(close_nr>=1+u && !equal(bracket[u],close[close_nr-1-u])) {
printf("Tag %u mismatch: %s/%s\n",u,bracket[u],close[close_nr-1-u]);
break;
}
}
if(u==bracket_nr)
if(bracket_nr==close_nr)
printf("\n** You have the correct number of brackets in your HTML text.\n\n");
else
printf("\n** Tags are missing.\n\n");
}
system ("PAUSE");
return 0;
};