Thread: HTML tags validator

  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
    Quote Originally Posted by root4 View Post
    ...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


    Hi,

    What if it's not allowed to use string.h ? for the strcmp part ? is there any other way to do it ?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Terrorist View Post
    Hi,

    What if it's not allowed to use string.h ? for the strcmp part ? is there any other way to do it ?
    Is this some sort of assignment, where you are allowed certain include files and not others?

    If so, write your own function that does exactly what strcpy does (or at least something close enough to know if the strings match or not).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Here's an alternative implementation of strcmp(), you need to provide 2
    C-strings (i.e. with the null char at the end); it returns 0 if the strings
    are not identical (or if no strings were provided) and 1 if the strings are
    equivalent.

    Code:
    int eqstr(const char* s1,const char* s2) {
      if(!s1 || !s2) return 0;
      while(*s1 && *s1++==*s2++);
      return !*s1 && !*s2;
    }

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    18
    Quote Originally Posted by matsp View Post
    Is this some sort of assignment, where you are allowed certain include files and not others?

    If so, write your own function that does exactly what strcpy does (or at least something close enough to know if the strings match or not).

    --
    Mats
    Actually it's my friends' assignment, but since long time no C for me, I wanted to do it myself, I have done most of the work here, but still the comparison part should be done.

    How does the strcmp function look like ? I mean what's its functionality exactly ? I though it uses subtraction or something like that ?

  15. #15
    Registered User
    Join Date
    May 2008
    Posts
    18
    Quote Originally Posted by root4 View Post
    Here's an alternative implementation of strcmp(), you need to provide 2
    C-strings (i.e. with the null char at the end); it returns 0 if the strings
    are not identical (or if no strings were provided) and 1 if the strings are
    equivalent.

    Code:
    int eqstr(const char* s1,const char* s2) {
      if(!s1 || !s2) return 0;
      while(*s1 && *s1++==*s2++);
      return !*s1 && !*s2;
    }
    So in other words, each row of those 2-D arrays should end with null char ? which is not possible, is it ?

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