Search code examples
8051keilc51

8051 sentence and word counter


I found this code below on the internet which is suppose to count the sentences on an 8051 MCU. Can someone please explain to me what is exactly happening where there are question marks. Any kind of help would be highly appreciated.

#include<string.h>

char code  *text=" what  is a program? that  has,   a   a lot of errors!  When "   ;     
char code  *text1=" you compile. this  file,   uVision. reports a number of? ";
char code  *text2=" problems that you! may interactively correct. "  ;  //Null characters are also included in array!!!

void count ( char pdata*  ,  char pdata*); 

void main (void){

char pdata   Nw,Ns;
char data TextNw[2],TextNs[2];
    count(&Nw, &Ns); // call subroutine
    TextNw[0]=Nw/10;   //?????????????????????????????????
    TextNw[1]=Nw%10;   //?????????????????????????????????
    TextNs[0]=Ns/10;   //?????????????????????????????????
    TextNs[1]=Ns%10;   //?????????????????????????????????

    while(1);

}


void count ( char pdata *Nw, char pdata  *Ns ){


unsigned char N, i, ch;
typedef enum  {idle1, idle2} state;   //?????????????????????????????????
state S;   // begining state


    P2=0x00;        // pdata bank definition it must be performed first!! 
    *Ns=*Nw=0;      // without proper start-up there is no initialisation, initialise now!! 
    S=idle1;        // beginning state
    N=strlen(text)+strlen(text1)+strlen(text2)+3; //????????????? + 3 to acount 3 Null characters!
    P2=0x00;                                      // pdata bank definition
    for(i=0;i!=N;i++){
        ch=text[i];                               // take a caharacter from the text
        switch (S)
        {
            case (idle1):{
                if (ch==0) break;                 // skip NULL terminating character!
                if (ch!=' '){
                            S=idle2;
                            (*Nw)++;
                            }
                break;
            }
            case(idle2):{
                if (ch==0) break;                  // skip NULL terminating character!
                if((ch==' ')||(ch==','))S=idle1;
                else if ((ch=='?')||(ch=='.')||(ch=='!')){
                                                        S=idle1;
                                                        (*Ns)++;
                                                        }
                break;
            }
          }

     }

} 

Solution

  • This program does 2 things in conjunction - counts number of sentences in the text and counts the number of words in the text. Once the counting is done, the results are stored in 2-char arrays. For example, for 57 words in 3 sentences the results will be stored as this: TextNw = {'5','7'} and TextNs = {'0','3'}.

    The variable N contains the full length of the text with the addition of 3 null terminating characters (one per sentence).

    The algorithm simultaneously counts words and sentences. In idle1 state the counting is in word-counting mode. In idle2 state the counting is in sentence-counting mode. The modes are interchanged according to current character being read - if delimiter is encountered, the appropriate counter is increased.