Thread: HTML tags validator

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    18

    Unhappy HTML tags validator

    Hello folks,

    I have started to write this simple program that reads from keyboard, stores the on tags (aka <html>) into a 2-D array, and stores the off-tags (aka. </html>) into another. then print out both arrays (stacks).

    The code contains 4 functions plus main function. two push functions, and 2 write tags functions.

    I am having a problem with storing / printing the OFF-TAGS of the original string.

    Could someone help me out in this asap

    Code:
    #include <stdio.h>
    #define B_OPEN '<'
    #define B_CLOSE '>'
    #define BRACKET_MAX 10
    #define B_CHAR_MAX 7
    
    char bracket[BRACKET_MAX][B_CHAR_MAX]={0};
    char close[BRACKET_MAX][B_CHAR_MAX]={0};
    int diagnosis=0;
    signed char bracket_nr, close_nr = 0;
    char close_char, c, bracket_char = 0;
    
    /********************  Prototypes ***********************/
    /********************************************************/
    /********************************************************/
    int push (void);
    int writetags (void);
    int push2(void);
    int writetags2 (void);
    
    /********************   Main      ***********************/
    /********************************************************/
    /********************************************************/
    int main (void){
    
        char c=0;
        bracket_nr=-1;
        close_nr=-1;
    
        diagnosis=0;
        while((c=getchar())!=EOF && c!='\n'){
            if(c==B_OPEN){
              if (c=='/'){diagnosis=push2();} //
                else if (c!='/')  {diagnosis=push();}
               
       }
     switch (diagnosis){
     case -3:
     printf("infinate bracket.\n");
     break;
     case -5:
     printf("Too Large Bracket.\n");
     break;
     case -7:
     printf("Too many bracket.\n");
     break;
     
      };
     if (diagnosis < 0)
     break;
     
     
     };
     if(diagnosis==0){
     printf("No brackets.\n");
     
     }else if (diagnosis > 0){
           printf("You have written %d brackets; they are:\n",diagnosis);
           writetags();
          
           writetags2(); 
           }else{
          printf("Text contains errors.\n");
           
           };
           system ("PAUSE");
           return diagnosis;
    
     };
    
    
    
    /******************** Push Function**********************/
    /********************************************************/
    /********************************************************/
         int push (void){
    
         char c = 0;
         char bracket_char =0;
         
         c=getchar();
         bracket_nr++;
         if(bracket_nr>=BRACKET_MAX)
         return (diagnosis = -7); //to many tags
         diagnosis=bracket_nr+1;
         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 stack bracket
       bracket[bracket_nr][bracket_char]=c;
       };
       bracket[bracket_nr][bracket_char]='\0';
       if(c==B_CLOSE){
         return diagnosis; 
       }else if (bracket_char>=B_CHAR_MAX){
         return(diagnosis=-5);
       }else if (c==EOF || c=='\n')
         return (diagnosis=-3);
       return diagnosis;
     };
     
     
     
     
     
    /******************** Push2 Function**********************/
    /********************************************************/
    /********************************************************/
         int push2 (void){
    
    
         char c =0;
         char close_char =0;
         
         c=getchar();
         close_nr++;
    
         if(close_nr>=BRACKET_MAX)
         return (diagnosis = -7); //to many tags
         diagnosis=close_nr+1;
         for(close_char=0;c!=EOF && c!='\n' && c!=B_CLOSE && close_char<B_CHAR_MAX;close_char++, c=getchar()){
       //record tag name into the stack bracket
       close[close_nr][close_char]=c;
       
       };
       close[close_nr][close_char]='\0';
       if(c==B_CLOSE){
     return diagnosis; 
       
       }else if (close_char>=B_CHAR_MAX){
      return(diagnosis=-5);
       
       }else if (c==EOF || c=='\n')
      return (diagnosis=-3);
       return diagnosis;
     };
     
     
     
    /********************  writetags function ***************/
    /********************************************************/
    /********************************************************/
    int writetags (void) {
    
    printf("The opening tags are:\n");
        
    for(;bracket_nr>=0; bracket_nr--){
        for(bracket_char=0; bracket[bracket_nr][bracket_char]!='\0'; bracket_char++){
       putchar(bracket[bracket_nr][bracket_char]);
      }
         putchar('\n');
    }
    return 0;  
    };
     
     
    
    
    /********************  writetags2 function ***************/
    /********************************************************/
    /********************************************************/
    int writetags2 (void) {
    
     printf("And the Closing Tags are:\n");
    for(;close_nr>=0; close_nr--){
        for(close_char=0; close[close_nr][close_char]!='\0'; close_char++){
       putchar(close[close_nr][close_char]);
      } 
         putchar('\n');
    }      
    };

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Please bold or denote where you are having the issue.. That and next time just attach the c file. I don't know about the rest of the people here, but I am 1,000 times more likely to actually read an entire program that is attached and I can compile or whatever, than one I have to copy and paste and reformat (and I say that while using IDE's that can format automatically).

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    18
    Hi,

    Attached the C file, and for where is the problem's cause, I am not really sure, but maybe:

    this bit:
    Code:
    while((c=getchar())!=EOF && c!='\n'){
            if(c==B_OPEN){
              if (c=='/'){diagnosis=push2();} //
                else if (c!='/')  {diagnosis=push();}
               
       }
    which is in the MAIN func. or the problem is in the function "push2".

    Thanks for looking.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if (c==B_OPEN)
    it cannot be '/' so the internal if has no meaning

    if (c=='/') ...

    else if (c!='/')

    second if has no sence
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    18

    Thumbs up

    Quote Originally Posted by vart View Post
    if (c==B_OPEN)
    it cannot be '/' so the internal if has no meaning

    if (c=='/') ...

    else if (c!='/')

    second if has no sence
    Thank You Very much, actually that was GREAT hint from you, I have solved the problem....Finally, Just added increment to the char C (c=getchar() before those nested IF's and now it works
    Last edited by Terrorist; 05-06-2008 at 09:07 AM.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    18
    Hi,

    I have solved the problem of separating the closing tags from the openeing tags and now I have got something like this:

    Code:
    <html><body>This is HTML</body></html>
    You have written 4 brackets; they are:
    
    The opening tags are:
    body
    html
    
    And the Closing Tags are:
    html
    body
    
    You have the correct number of brackets in your HTML text.
     Press any key to continue . . .

    I have removed the "/" from the closing brackets for comparison purposes.

    Now I have got this problem, I need to compare both arrays to validate the HTML string, So I have to reverse the Closing one and then do 2 nested for loops for tags order check and tags syntax check. However, it doesn't work with me. I have done this function to do the comparison, can someone please check it for me.

    Notice that the 1st for loop reads from row=0 and increments for the 1st array, WHILE it starts from the max and decrements for the 2nd one to read in reverse order. Not sure though.

    Code:
     
    int compare1 (void)
    {
    
    
        
    for(bracket_nr=0;bracket_nr!='\0' && close_nr>=0; bracket_nr++, close_nr--){  
           for(bracket_char=0, close_char=0; bracket[bracket_nr][bracket_char]!='\0' && close[close_nr][close_char]!='\0'; bracket_char++, close_char++){
                               
    if (close[close_nr][close_char]!=bracket[bracket_nr][bracket_char]){printf("\nYou have errors at:\n&#37;c");}
                               
    }}
     
    return 0;    
    }

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    18
    Hi,

    here is the final code, but the 'compare' function doesn't seem to work. Can someone please help me in this.

    Code:
    #include <stdio.h>
    #define B_OPEN '<'
    #define B_CLOSE '>'
    #define BRACKET_MAX 10
    #define B_CHAR_MAX 7
    
    char bracket[BRACKET_MAX][B_CHAR_MAX]={0}; //THE OPENING BRACKETS ARRAY
    char close[BRACKET_MAX][B_CHAR_MAX]={0};  //THE CLOSING BRACKETS ARRAY
    int diagnosis, diag=0;                   // INTEGER VALUES TO COUNT THE BRACKET NUMBERS IN BOTH ARRAYS
    signed char bracket_nr, close_nr = 0; //ROW COUNT FOR BOTH ARRAYS
    char close_char, c, bracket_char = 0; //COLUMN COUNT FOR BOTH ARRAYS
    
    /********************  Prototypes ***********************/
    /********************************************************/
    /********************************************************/
    int push (char c);  //TO PUSH TO THE OPENING BRACKETS ARRAY
    int push2(char c);//TO PUSH TO THE CLOSING BRACKETS ARRAY
    int writetags (void); //TO POP FROM THE OPENING BRACKETS ARRAY
    int writetags2 (void); //TO POP FROM THE CLOSING BRACKETS ARRAY
    int compare(void); //TO COMPARE BOTH ARRAYS
    
    /********************   Main      ***********************/
    /********************************************************/
    /********************************************************/
    int main (void){
    
        char c=0;
        bracket_nr=-1;
        close_nr=-1;
    
        diagnosis=0;
        while((c=getchar())!=EOF && c!='\n'){
            if(c==B_OPEN){
              c=getchar();
              if (c=='/'){diagnosis=push2(c);} // THIS TO SEPARATE OPENING FROM CLOSING ARRAYS
                else  diag=push(c);
               
       }
     switch (diagnosis+diag){
     case -3:
     printf("infinate bracket.\n");
     break;
     case -5:
     printf("Too Large Bracket.\n");
     break;
     case -7:
     printf("Too many bracket.\n");
     break;
     
      };
     if ((diagnosis+diag) < 0)
     break;
     
     
     };
     if((diagnosis+diag)==0){
     printf("No brackets.\n");
     
     }else if ((diagnosis+diag) > 0){
      printf("****************************************\n");
      printf("You have written %d brackets; they are:\n",(diagnosis+diag));
          
      writetags(); 
      writetags2(); 
      compare();     
      if(diagnosis==diag) {printf("\nYou have the correct number of brackets in your HTML text.\n\n");}
       else if (diagnosis>diag){printf("\nOne or more of the OPENING tags is missing: %d tag(s)\n ", diagnosis-diag);}
        else if (diagnosis<diag){printf("\nOne or more of the CLOSING tags is missing: %d tag(s)\n ", diag-diagnosis);}
              
      }else{
      printf("Text contains errors.\n");
           
      };
           
    system ("PAUSE");
    return (diagnosis+diag);
      
     };
    
    
    /******************** Push Function**********************/
    /********************************************************/
    /********************************************************/
    int push (char c1){
    
         //char c1 = 0;
    char bracket_char =0;
         
         //c1=getchar();
    bracket_nr++;
      if(bracket_nr>=BRACKET_MAX)
        return (diag = -7); //to many tags
        diagnosis=bracket_nr+1;
         for(bracket_char=0;c1!=EOF && c1!='\n' && c1!=B_CLOSE && bracket_char<B_CHAR_MAX;bracket_char++, c1=getchar()){
       //record tag name into the stack bracket
           bracket[bracket_nr][bracket_char]=c1;
         };
       bracket[bracket_nr][bracket_char]='\0';
       if(c1==B_CLOSE){
         return diagnosis; 
       }else if (bracket_char>=B_CHAR_MAX){
         return(diagnosis=-5);
       }else if (c1==EOF || c=='\n')
         return (diagnosis=-3);
         
       return diagnosis;
     };
     
     
     
     
     
    /******************** Push2 Function**********************/
    /********************************************************/
    /********************************************************/
    int push2 (char c2){
    
    
         //char c =0;
    char close_char =0;
         
    c2=getchar();  //it removes the '/' from the closing tag.
    close_nr++;
    
      if(close_nr>=BRACKET_MAX)
        return (diagnosis = -7); //to many tags
        diagnosis=close_nr+1;
         for(close_char=0;c2!=EOF && c2!='\n' && c2!=B_CLOSE && close_char<B_CHAR_MAX;close_char++, c2=getchar()){
       //record tag name into the stack bracket
         close[close_nr][close_char]=c2;
       };
       close[close_nr][close_char]='\0';
       if(c2==B_CLOSE){
     return diagnosis; 
       
       }else if (close_char>=B_CHAR_MAX){
      return(diagnosis=-5);
       
       }else if (c2==EOF || c=='\n')
      return (diagnosis=-3);
    
       return diagnosis;
     };
     
     
     
    /********************  writetags function ***************/
    /********************************************************/
    /********************************************************/
    int writetags (void) {
    
    
    printf("\nThe Opening Tags Are:\n");
        
    for(;bracket_nr>=0; bracket_nr--){
       for(bracket_char=0; bracket[bracket_nr][bracket_char]!='\0'; bracket_char++){
         putchar(bracket[bracket_nr][bracket_char]);
      }
         putchar('\n');
    }
    return 0;  
    };
    
    
    
    
    /********************  writetags2 function ***************/
    /********************************************************/
    /********************************************************/
    int writetags2 (void) {
    
    printf("\nAnd the Closing Tags Are:\n");
      for(;close_nr>=0; close_nr--){
         for(close_char=0; close[close_nr][close_char]!='\0'; close_char++){
         putchar(close[close_nr][close_char]);
     
      } 
    
         putchar('\n');
    
    }
    return 0;
    };
     
     
     
    /********************  Compare function ***************/
    /********************************************************/
    /********************************************************/
    
    int compare(void){
    
    for (;bracket_nr>=0;bracket_nr--){
        for (close_nr=0;;close_nr++){
    
                    
                     }
                     if (bracket[bracket_nr][bracket_char]!=close[close_nr][close_char]){
                     printf("this tag is incorrect: ");                                                                   
                     printf("%c%c",bracket[bracket_nr][bracket_char], close[close_nr][close_char]);
                     putchar('\n');}
                     }
    
       return 0; 
        
    }

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Notice that your bracket_nr and close_nr counters are cleared by the writetags() functions
    (this is not a good idea as you need them later in compare()).

    Besides this section in the compare() function is a bit odd (probably not what you intended to do):
    Code:
       ...
        for (close_nr=0;;close_nr++){
                     }
       ...
    EDIT: at last you need to compare the full tag names, not only one char.
    Last edited by root4; 05-07-2008 at 04:37 PM.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    18
    Quote Originally Posted by root4 View Post
    Notice that your bracket_nr and close_nr counters are cleared by the writetags() functions
    (this is not a good idea as you need them later in compare()).

    Besides this section in the compare() function is a bit odd (probably not what you intended to do):
    Code:
       ...
        for (close_nr=0;;close_nr++){
                     }
       ...
    EDIT: at last you need to compare the full tag names, not only one char.
    So what do you think I need to do in here ? I am working on two different codes at the same time, almost lost in this one.
    Last edited by Terrorist; 05-07-2008 at 05:19 PM.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    So what do you think I need to do in here?
    ...you're the one giving the specifications:
    "I need to compare both arrays to validate the HTML string, So I have to reverse the Closing one and then do 2 nested for loops for tags order check and tags syntax check."

    I just pointed out some possible problems in your code, try to fix them.

    Here the pseudo code:
    (you don't really need to reverse anything, you can walk an array backward)
    Code:
    repeat:
      pop last opening tag
      pop first closing tag
      if(strcmp(opening tag name,closing tag name)!=0) error, tags mismatch
    end_repeat

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    18
    Here it is:

    Code:
     #include <stdio.h>
    #define B_OPEN '<'
    #define B_CLOSE '>'
    #define BRACKET_MAX 10
    #define B_CHAR_MAX 7
    
    char bracket[BRACKET_MAX][B_CHAR_MAX]={0}; //THE OPENING BRACKETS ARRAY
    char close[BRACKET_MAX][B_CHAR_MAX]={0};  //THE CLOSING BRACKETS ARRAY
    int diagnosis, diag=0;                   // INTEGER VALUES TO COUNT THE BRACKET NUMBERS IN BOTH ARRAYS
    signed char bracket_nr, close_nr = 0; //ROW COUNT FOR BOTH ARRAYS
    char close_char, c, bracket_char = 0; //COLUMN COUNT FOR BOTH ARRAYS
    int close_counter, open_counter;
    /********************  Prototypes ***********************/
    /********************************************************/
    /********************************************************/
    int push (char c);  //TO PUSH TO THE OPENING BRACKETS ARRAY
    int push2(char c);//TO PUSH TO THE CLOSING BRACKETS ARRAY
    int writetags (void); //TO POP FROM THE OPENING BRACKETS ARRAY
    int writetags2 (void); //TO POP FROM THE CLOSING BRACKETS ARRAY
    int compare(const char* ,const char* ); //TO COMPARE BOTH ARRAYS
    
    /********************   Main      ***********************/
    /********************************************************/
    /********************************************************/
    int main (void){
    printf("Please Enter Your HTML statement..\n\n\n");
        char c=0;
        bracket_nr=-1;
        close_nr=-1;
        close_counter=0;
        open_counter=0;
        diagnosis=0;
        while((c=getchar())!=EOF && c!='\n'){
            if(c==B_OPEN){
              c=getchar();
              if (c=='/'){diagnosis=push2(c);} // THIS TO SEPARATE OPENING FROM CLOSING ARRAYS
                else  diag=push(c);
               
       }
     switch (diagnosis+diag){
     case -3:
     printf("infinate bracket.\n");
     break;
     case -5:
     printf("Too Large Bracket.\n");
     break;
     case -7:
     printf("Too many bracket.\n");
     break;
     
      };
     if ((diagnosis+diag) < 0)
     break;
     
     
     };
     if((diagnosis+diag)==0){
     printf("No brackets.\n");
     
     }else if ((diagnosis+diag) > 0){
     
      printf("\n\n** You have written &#37;d brackets, \n",(diagnosis+diag));
            
      writetags(); 
      writetags2(); 
      
      for (bracket_nr=0; bracket_nr<=open_counter && close_nr>=0; bracket_nr++, close_nr--){
      compare(bracket[bracket_nr][bracket_char], close[close_nr][close_char]); }
    
      if(diagnosis==diag) {printf("\n** You have the correct number of brackets in your HTML text.\n\n");}
       else if (diagnosis>diag){printf("\n** One or more of the OPENING tags is missing: %d tag(s)\n ", diagnosis-diag);}
        else if (diagnosis<diag){printf("\n** One or more of the CLOSING tags is missing: %d tag(s)\n ", diag-diagnosis);}
              
      }else{
      printf("Text contains errors.\n");
           
      };
           
    system ("PAUSE");
    return (diagnosis+diag);
      
     };
    
    
    /******************** Push Function**********************/
    /********************************************************/
    /********************************************************/
    int push (char c1){
    
         //char c1 = 0;
    char bracket_char =0;
         
         //c1=getchar();
    bracket_nr++;
    open_counter++;
      if(bracket_nr>=BRACKET_MAX)
        return (diag = -7); //to many tags
        diagnosis=bracket_nr+1;
         for(bracket_char=0;c1!=EOF && c1!='\n' && c1!=B_CLOSE && bracket_char<B_CHAR_MAX;bracket_char++, c1=getchar()){
       //record tag name into the stack bracket
           bracket[bracket_nr][bracket_char]=c1;
           
         };
       bracket[bracket_nr][bracket_char]='\0';
       if(c1==B_CLOSE){
         return diagnosis; 
       }else if (bracket_char>=B_CHAR_MAX){
         return(diagnosis=-5);
       }else if (c1==EOF || c=='\n')
         return (diagnosis=-3);
         
       return diagnosis;
     };
     
     
     
     
     
    /******************** Push2 Function**********************/
    /********************************************************/
    /********************************************************/
    int push2 (char c2){
    
    
         //char c =0;
    char close_char =0;
         
    c2=getchar();  //it removes the '/' from the closing tag.
    close_nr++;
    close_counter++;
    //printf("\n\nErrors in Closing Brackets,  (if They Are Any) are :\n");  // KRIS: JUST A PRINTF STATEMENT
      
      if(close_nr>=BRACKET_MAX)
        return (diagnosis = -7); //to many tags
        diagnosis=close_nr+1;
    
         
         for(close_char=0;c2!=EOF && c2!='\n' && c2!=B_CLOSE && close_char<B_CHAR_MAX;close_char++, c2=getchar()){
       //record tag name into the stack bracket
         close[close_nr][close_char]=c2;
      
       };
       
       close[close_nr][close_char]='\0'; 
           
           
       if(c2==B_CLOSE){
     return diagnosis; 
       
       }else if (close_char>=B_CHAR_MAX){
      return(diagnosis=-5);
       
       }else if (c2==EOF || c=='\n')
      return (diagnosis=-3);
    
       return diagnosis;
     };
     
     
     
    /********************  writetags function ***************/
    /********************************************************/
    /********************************************************/
    int writetags (void) {
    
    
    printf("\n\n%d Of Which is/Are Opening Tags; and it's/they are :\n",  open_counter);
        
    for(;bracket_nr>=0; bracket_nr--){
       for(bracket_char=0; bracket[bracket_nr][bracket_char]!='\0'; bracket_char++){
         putchar(bracket[bracket_nr][bracket_char]);
      }
         putchar('\n');
    }
    
    return 0;  
    };
    
    
    
    
    /********************  writetags2 function ***************/
    /********************************************************/
    /********************************************************/
    int writetags2 (void) {
    
    printf("\n\nWhile %d is/are Closing Tags; And It's/They are:\n", close_counter);
      for(;close_nr>=0; close_nr--){
         for(close_char=0; close[close_nr][close_char]!='\0'; close_char++){
         putchar(close[close_nr][close_char]);
        
      } 
    
         putchar('\n');
    
    }
    
    return 0;
    };
     
     
    
    /********************  compare function ***************/
    /********************************************************/
    /********************************************************/
    
     int compare(const char* s1,const char* s2)
     {
    
                
               
      if(!s1 || !s2) return 0;
      while(*s1 && *s1++==*s2++);
      return !*s1 && !*s2;
    
    
        
         
    }
    Last edited by Terrorist; 05-08-2008 at 12:31 PM.

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    18
    With these warnings in compilation:
    68 C:\Dev-Cpp\Projects\bracket_final_testing_with_comp.c [Warning] passing arg 1 of `compare' from incompatible pointer type
    68 C:\Dev-Cpp\Projects\bracket_final_testing_with_comp.c [Warning] passing arg 2 of `compare' from incompatible pointer type


    Edited the loop to pop one from the bottom, and another from the top:

    Code:
    for (bracket_nr=0; bracket_nr<=open_counter && close_nr>=0; bracket_nr++, close_nr--){
      compare(bracket[bracket_nr][bracket_char], close[close_nr][close_char]); }
    Last edited by Terrorist; 05-08-2008 at 11:56 AM.

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    18
    P.S. The lines with bold and red fonts are the new additions:

    Function "compare" declaration,
    Function call.
    and function definition.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    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;
    };

  15. #15
    Registered User
    Join Date
    May 2008
    Posts
    18

    Thumbs up

    Thanks root4, that's pretty neat, at least as optimized as possible.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Library which extract html tags content
    By Bargi in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2007, 10:17 PM
  2. Help reading file and adding html tags
    By enhancedmode in forum C Programming
    Replies: 3
    Last Post: 05-30-2005, 03:02 PM
  3. Help w/ HTML Tags
    By Landroid in forum C++ Programming
    Replies: 5
    Last Post: 03-08-2005, 08:19 PM
  4. Stacks, classes, HTML tags, and parsing.
    By Shinobi-wan in forum C++ Programming
    Replies: 5
    Last Post: 10-01-2003, 05:50 PM
  5. HTML tags
    By netboy in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-27-2002, 07:52 AM

Tags for this Thread