Hi Again,

If I have this 2-D array stored in s1[j] :

one
      two
      three
      four

And I wanted to copy the whole row IF the first letter of that row ==o . How Can I do that ? assuming that the destination array is s2[k][l].


I am confused because whenever I try to do it, it copies only the letter o and leaves the rest.The condition !='\n' doesn't seem to work ?!

post the code you have tried. When the first letter is 'o' then just call strcpy() to copy the entire word. But whether that will work or not all depends on how you declared the two arrays. Post how you declared the two arrays and the rest of your program.

Hi Ancient Dragon,

Frankly Speaking I have posted the whole code 10's of times here: http://www.daniweb.com/forums/thread117127.html

Though some said it was badly formatted so I though to make things simpler to post only the problem.


And because I don't want you to get confused of that code, I will post only the part where it's supposed to copy from an array to another:

int writetags (void) {
     extern char bracket[BRACKET_MAX][B_CHAR_MAX];
     char close[BRACKET_MAX][B_CHAR_MAX];
     extern int diagnosis;
     extern signed char bracket_nr, close_nr;
     char bracket_char, close_char;
     
     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');
           
           }

/*****************************************START: TO COPY and PRINT CLOSING TAGS*************************************/

// FIRST: check if there is a slash, then move the row to new array called 'close'
close_nr=0;
for(;bracket_nr>=0; bracket_nr--, close_nr++){
    if (bracket_nr=='/'){
       for(close_char=0, bracket_char=0; bracket[bracket_nr][bracket_char]!='\n'; bracket_char++, close_char++){                                            
           close[close_nr][close_char]=bracket[bracket_nr][bracket_char];}}}
           
close[close_nr][close_char]='\0';

// Then Print out the new array 'close' that contains the HTML closing tags only.
printf("Off 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');
           
           }
 /*****************************************END: TO COPY and PRINT CLOSING TAGS*************************************/         
};

As you can see, this function will 1: print the contents of the original Stack (2-D array), then try to Check if any ROW starts with this '/', then copy the whole ROW to new 2-D array.

Thanks in Advance.

And here is the whole code if you wanna copy and test it on your compiler:

#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];
int diagnosis;
signed char bracket_nr, close_nr;


/********************  Prototypes ***********************/
/********************************************************/
/********************************************************/
int push (void);
int writetags (void);



/********************   Main      ***********************/
/********************************************************/
/********************************************************/
int main (void){
    extern int diagnosis;
    extern signed char bracket_nr;
    char c;
    bracket_nr=-1;
    diagnosis=0;
    while((c=getchar())!=EOF && c!='\n'){
        if(c==B_OPEN){
        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();
       }else{
      printf("Text contains errors.\n");
       
       };
       system ("PAUSE");
       return diagnosis;
       
        
        };
        
        
        
/******************** Push Function**********************/
/********************************************************/
/********************************************************/
        int push (void){
     extern char bracket [BRACKET_MAX][B_CHAR_MAX];
     extern int diagnosis;
     extern signed char bracket_nr;
     char c, bracket_char;
     
     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;
        };
        
/********************  writetags function ***************/
/********************************************************/
/********************************************************/
     int writetags (void) {
     extern char bracket[BRACKET_MAX][B_CHAR_MAX];
     char close[BRACKET_MAX][B_CHAR_MAX];
     extern int diagnosis;
     extern signed char bracket_nr, close_nr;
     char bracket_char, close_char;
     
     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');
           
           }

/*****************************************START: TO COPY and PRINT CLOSING TAGS*************************************/
close_nr=0;
for(;bracket_nr>=0; bracket_nr--, close_nr++){
    if (bracket_nr=='/'){
       for(close_char=0, bracket_char=0; bracket[bracket_nr][bracket_char]!='\n'; bracket_char++, close_char++){                                            
           close[close_nr][close_char]=bracket[bracket_nr][bracket_char];}}}
           
close[close_nr][close_char]='\0';
printf("Off 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');
           
           }
 /*****************************************END: TO COPY and PRINT CLOSING TAGS*************************************/         
};

Not sure if I can answer your specific question, but a couple observations:

line 121: the value of bracket_nr is -1 when that line is hit, so the loop does nothing

line 126: The value of close_char is -52, which of course is an illegal index value.

line 130: array close is uninitialized and just contains random data. You should initialize it to 0 when declared.

Thank You for Your useful notes, let me check the reasons and reply later with corrections, but how did you notice those values ? Debugger ?

Yes, debugger show me the values. I use VC++ 2008 Express, which has the best debugger I have ever found.

Damnit I've never got stuck on code like this time :SSSSSSS

Did anyone of you guys see any code to validate HTML tags using C before ?

looks like one problem is the uninitialized variables on line 107. On line 109 you are trying to compare the uninitialized variable with 0. Not a good thing to do. :)

But those lines work without problems, actually variables in line 106 are initialized in the main.

The problem is with the code between lines 120-135 :(

Here is the output I get

<HTML></BODY>Whatever text<BODY></HTML>
You have written 4 brackets; they are:
/HTML
BODY
/BODY
HTML
Off tags are:

Press any key to continue . . .

And here is the code that I modified. I deleted all extern statements because they are not necessary and initialized all data and arrays to 0. The extern statements were the cause of the wierd numbers I found earlier.

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

#define B_OPEN '<'
#define B_CLOSE '>'
#define BRACKET_MAX 10
#define B_CHAR_MAX 7

char bracket[BRACKET_MAX][B_CHAR_MAX] = {0};
int diagnosis = 0;
signed char bracket_nr = 0, close_nr = 0;


/********************  Prototypes ***********************/
/********************************************************/
/********************************************************/
int push (void);
int writetags (void);



/********************   Main      ***********************/
/********************************************************/
/********************************************************/
int main (void){
//    extern int diagnosis;
//    extern signed char bracket_nr;
    char c = 0;
    bracket_nr=-1;
    diagnosis=0;
    while((c=getchar())!=EOF && c!='\n'){
        if(c==B_OPEN){
        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();
       }else{
      printf("Text contains errors.\n");
       
       };
       system ("PAUSE");
       return diagnosis;
       
        
        };
        
        
        
/******************** Push Function**********************/
/********************************************************/
/********************************************************/
        int push (void){
//     extern char bracket [BRACKET_MAX][B_CHAR_MAX];
//     extern int diagnosis;
//     extern signed char bracket_nr;
     char c = 0, 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;
        };
        
/********************  writetags function ***************/
/********************************************************/
/********************************************************/
     int writetags (void) {
//     extern char bracket[BRACKET_MAX][B_CHAR_MAX];
         char close[BRACKET_MAX][B_CHAR_MAX] = {0};
//     extern int diagnosis;
//     extern signed char bracket_nr, close_nr;
     char bracket_char = 0, close_char = 0;
     
     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');
           
           }

/*****************************************START: TO COPY and PRINT CLOSING TAGS*************************************/
close_nr=0;
for(;bracket_nr>=0; bracket_nr--, close_nr++){
    if (bracket_nr=='/'){
       for(close_char=0, bracket_char=0; bracket[bracket_nr][bracket_char]!='\n'; bracket_char++, close_char++){                                            
           close[close_nr][close_char]=bracket[bracket_nr][bracket_char];}}}
           
close[close_nr][close_char]='\0';
printf("Off 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');
           
           }
 /*****************************************END: TO COPY and PRINT CLOSING TAGS*************************************/         
      return 0;
};

Hi,


I have re-written the code to a simpler one, 2 push functions, 2 pop (and print out) functions, but I seriously wonder why it doesn't work as it's supposed to do :very angry face smiley:


Here is the code if anyone interested to pull his hair like me :s

#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){
          diagnosis=push();
           if (c=='/'){diagnosis=push2();}
   }
 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();
       }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) {
    
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');
}
printf("And the Closing Tags are:\n");
writetags2();   
};
 
 


/********************  writetags2 function ***************/
/********************************************************/
/********************************************************/
int writetags2 (void) {

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');
}      
};

I've just seen your reply Dragon, let me check that and reply later :)

thanks.

Hi,

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

<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.

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%c");}
                           
}}
 
return 0;    
}

Sorry, Cause I want to annoy you with the same question.

<hbr> does not have an ending tag.
<I>, <a>, <body> tags needs or may have attributes.

if you are checking the beginning tag & ending tag, how will you handle this?

Sorry, If my question irritates you

Sorry, Cause I want to annoy you with the same question.

<hbr> does not have an ending tag.
<I>, <a>, <body> tags needs or may have attributes.

if you are checking the beginning tag & ending tag, how will you handle this?

Sorry, If my question irritates you

There are many tags that can be used unpaired in HTML, but in this program, it is not a must to stick with default HTML tags, any tags would work but the condition is if you have an opening tag, there should be a closing Tag for it. Let's say it is a HTML-LIKE tags program.

But if I want to do that, I should add a static variable that contains all the unpaired tags, and when separating the open from the close tags, there should be a condition (if not from the static unpaired), then count into open or close, else, count into unpaired.

As for the tags with text body, it will be removed.

That should make sence.
Well then have a nice day

Hi again :D


I have finally done almost 95% of the program, it reads from the string, separates the opening tags from the closing tags and stores each category in separate array.

It checks the number of open brackets and closed ones and checks the pairing, if not paired in number, it shows that.

Now I have tried to do a new function that compares both arrays and checks:
1- the syntax differences between each pair (aka: <html></hmtl), AND
2- checks the pairing of the tags in term of type (aka: <html> should have </html>).

I have tried many ways but it didn't work properly, the following code would give a result of this string: <html><body>TEST</body></html> as:

<html><body>test</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 . . .

If you notice here that one array is the inverse of the other, how to do the comparison row by row and column by column without the need for another function that inverts one of them ?


The whole code so far is:

#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(); 
       
  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');

}    
};
 
 
 
/********************  Compare function ***************/
/********************************************************/
/********************************************************/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.