Thread: Error counting occurrences

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    26

    Error counting occurrences

    Hi to All,

    Foollowing Your suggestion, I have written this code:


    Code:
    #include <stdio.h> 
    #include <string.h> 
    #include <malloc.h> 
     
    #define BUFLEN 100 
     
    struct node 
    { 
           char info[BUFLEN] ; 
           struct node *pun ; 
     
    } ; 
     
     
     
    typedef struct node* newlist ; 
     
    void insert_myString (newlist *test, const char *buf); 
    int wordCounter(char *a, char *b); 
     
    int main( void ) 
    { 
        char myString1[ ] = "this is the first string and after is the 
    second string"; 
        char myString2[ ] = "this is the second string"; 
        char myString3[ ] = "this is the third string; no strings after 
    this string"; 
        newlist mytest = NULL ; 
        char myWord[ ] = "string"; 
        int counter = 0; 
        int n = 0; 
     
        insert_myString(&mytest, myString1) ; 
        insert_myString(&mytest, myString2) ; 
        insert_myString(&mytest, myString3) ; 
     
         while (mytest != NULL) 
         { 
            counter = wordCounter(mytest->info, myWord); 
             n = n + counter; 
             mytest = mytest->pun ; 
         } 
        printf("The occurrences number of word 'string' in the entire 
    struct is: %d\n", n); 
        sleep(3); 
     
    } 
     
     
     
    void insert_myString(newlist *test, const char *buf) 
    { 
         struct node *prec = *test ; 
     
         // create element 
         struct node *p = (newlist) malloc(sizeof(struct node)) ; 
     
         if (p == NULL) { 
             fprintf(stderr, "Error: memory allocation\n" ) ; 
             exit(-1) ; 
         } 
     
         strcpy(p->info, buf) ; 
         p->pun = NULL ; // init of last element 
     
         // the list is empty 
         if (prec == NULL) 
             *test = p ; 
         else { 
             while (prec->pun != NULL) 
                 prec = prec->pun ; 
     
             prec->pun = p ; 
         } 
     
    } 
     
     
     
    int wordCounter(char *a, char *b) 
    { 
        int cnt = 0; 
        char *sptr = a; 
        while ( *sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL ) 
        { 
            cnt++; 
            sptr += strlen( b ); 
        } 
        return(cnt); 
     }
    After I have compiled the program and it's work fine.

    Now, I have inserted the same solution in a my little server program (sorry for the lenght)


    Code:
    #include <stdio.h> 
    #include <string.h> 
    #include <sys/types.h> 
    #include <sys/socket.h> 
    #include <netinet/in.h> 
    #include <errno.h> 
    #include <malloc.h> 
     
    #define MYPORT 10000 
    #define MAXCONN 5 
    #define BUFLEN 100 
     
    struct node 
    { 
           char info[BUFLEN] ; 
           struct node *pun ; 
     
    } ; 
     
     
     
    typedef struct node* newlist ; 
     
    typedef int Boolean; 
     
    /* Prototype */ 
    void insertList (newlist *test, const char *buf); 
    int occurr_counter(newlist p, const char *buf); 
    int word_counter(char *a, const char *b); 
     
    int main(int argc, char *argv[]) 
    { 
      struct sockaddr_in myServer, Client; 
      int socketfd, newsocketfd, client_len, char_recv, i, n, occur; 
      newlist newtest = NULL ; 
      char c; 
      char myString[BUFLEN]; 
      char myWord[20]; 
      Boolean done = 0; 
      char terminator[3]={'.','.','\n'}; 
     
    /* local socket descriptor */ 
      if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 
              perror("system call socket failed"); 
              exit(1); 
      } 
     
      memset ( &myServer, 0, sizeof(myServer) ); 
     
      myServer.sin_family = AF_INET; 
      myServer.sin_addr.s_addr = htonl(INADDR_ANY); 
      myServer.sin_port = htons(MYPORT); 
     
      if (bind(socketfd, (struct sockaddr*) &myServer, sizeof(myServer)) 
    == -1) { 
              perror("system call bind failed"); 
              exit(2); 
      } 
     
      listen (socketfd, MAXCONN); 
     
      while (1) { 
              client_len = sizeof(Client); 
              if ((newsocketfd = accept(socketfd, (struct sockaddr *)&Client, 
    &client_len)) < 0) { 
                      perror("Connection accept error"); 
                      exit(3); 
              } 
              printf("Connection Opened.\r\n"); 
              send(newsocketfd, "Wellcome!\r\n", 11, 0); 
              send(newsocketfd, "Send to me Your strings !\r\n", 24, 0); 
              send(newsocketfd, "Close the last with .. !\r\n", 34, 0); 
     
         while (!done) 
             { 
                         i=0; 
                         char_recv = recv(newsocketfd, &c, 1, 0); 
                         while (c !='\n') 
                         { 
                                 myString[i++]=c; 
                                 char_recv = recv(newsocketfd, &c, 1, 0); 
                         } /* End while (c !='\n') */ 
                         myString[i]='\0'; 
     
               /* insert myString in the struct */ 
                   insertList(&newtest, myString) ; 
     
              /* Is the last ? */ 
              if (i > 2 && memcmp(&myString[i - 3], terminator, 2) == 0) 
    { 
                 done = 1; 
              } /* End di if */ 
         } /* while (!done) */ 
     
         /* Query myWord */ 
         send(newsocketfd,"Wich word will You to search?\r\n",30,0); 
              i=0; 
              char_recv = recv(newsocketfd, &c, 1, 0); 
              while (c !='\n') 
                      { 
                             myWord[i++]=c; 
                             char_recv = recv(newsocketfd, &c, 1, 0); 
                      } /* End while (c !='\n') */ 
              myWord[i]='\0'; 
     
         /* I search the occurrences of myWord in the entire struct */ 
         occur = occur_counter(newtest, myWord) ; 
     
              /* Connection closed */ 
         send(newsocketfd,"Now I close the connection\r\n",24,0); 
     
         sleep(3); 
     
             close(newsocketfd); 
             printf("Connection closed.\r\n"); 
            } 
     
    } 
     
     
     
    int occur_counter(newlist p, const char *buf) 
    { 
      int n, occ; 
     
         occ = 0; 
         n = 0; 
         while (p != NULL) 
         { 
               printf("Element: %s\n",p->info); 
              occ = word_counter(p->info, buf); 
              n = n + occ; 
              p = p->pun ; 
         } 
         return(n) ; 
     
    } 
     
     
     
    void insertList (newlist *test, const char *buf) 
    { 
         struct node *prec = *test ; 
     
         // element creation 
         struct node *p = (newlist) malloc(sizeof(struct node)) ; 
     
         if (p == NULL) { 
             fprintf(stderr, "'allocation memory error\n" ) ; 
             exit(-1) ; 
         } 
     
         strcpy(p->info, buf) ; 
         p->pun = NULL ; 
     
         // the list is empty? 
         if (prec == NULL) 
             *test = p ; 
         else { 
             while (prec->pun != NULL) 
                 prec = prec->pun ; 
     
             prec->pun = p ; 
         } 
     
    } 
     
     
     
    int word_counter(char *a, const char *b) 
    { 
        int cnt = 0; 
        char *sptr = a; 
     
        printf("%s\n", sptr); 
        while ( *sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL ) 
        { 
            cnt++; 
            sptr += strlen( b ); 
        } 
     
        printf( "%d\n", cnt ); 
        return(cnt); 
     
    };
    I have compiled the program (used cygWin) without errors. From cygwin I run this program and with windows telnet (telnet remote_IP 10000) I send to server the same strings of first sample.
    I have tested and the server receive and store correctly the string
    sended from client.

    When I search the occurrences of word "string", the server return a
    mistaken number for each string.

    Have You an "idea" ? I hope in Your help.
    Thank You and Best Regards
    Nick

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    void insert_myString(newlist *test, const char *buf) 
    { 
         struct node *prec = *test ; 
     
         // create element 
         struct node *p = (newlist) malloc(sizeof(struct node)) ; 
     
         if (p == NULL) { 
             fprintf(stderr, "Error: memory allocation\n" ) ; 
             exit(-1) ; 
         } 
     
         strcpy(p->info, buf) ;
    p->info isn't initialized, let alone allocated, but you copy a string to it. Not a good idea. (The same code also occurs in insertList().)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 03-07-2011, 01:24 AM
  2. Replies: 13
    Last Post: 08-02-2007, 08:47 AM
  3. Replies: 11
    Last Post: 12-11-2005, 11:50 PM
  4. char occurrences
    By viaxd in forum C Programming
    Replies: 6
    Last Post: 09-18-2003, 02:19 AM
  5. Program to show the number of occurrences of words...
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 01-26-2002, 06:44 PM