char occurrences

This is a discussion on char occurrences within the C Programming forums, part of the General Programming Boards category; Hello, i wrote a simple program to count the number of occurrences of char in file from ' ' to ...

  1. #1
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246

    char occurrences

    Hello, i wrote a simple program to count the number of occurrences of char in file from ' ' to '~' (by ascii) , i tested it and it seemed to work fine but then i tested it with another file and got strange results
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define SIZ 95
    void abz(char *buf, int *res);
    
    int main( int argc, char *argv[] )  {
    
    char buf[BUFSIZ];
    int i,len,temp;
    int total = 0;
    int lines = 0;
    int res[SIZ];
    FILE *fp;
    
    
    for(temp = 0; temp < SIZ; temp++)
    res[temp] = 0;
    
    
    if(argc < 2)
      {
        printf("Include Filename\n");
        exit(EXIT_FAILURE);
      }
    
    if((fp = fopen ( argv[1] , "r")) == NULL)
      {
        fprintf(stderr,"Cannot open file\n");
        exit(EXIT_FAILURE);
      }
    
    while(fgets(buf, BUFSIZ, fp) != NULL) {
    
    len = strlen (buf);
    
    total += len;
    
    ++lines;
    abz(buf,res);
    
    }
    
    fclose(fp);
    
    printf("\t%d chars\n",total);
    printf("\t%d lines\n",lines);
    
    printf("\n\tThe Letter Chart\n");
    
    for(i = ' ', temp = 0; i < '~', temp < SIZ; i++, temp++) {
    
    if( temp % 5 == 0 )
    printf("\n");
    
    printf("\t ' %c '= %d ",i,res[temp]);
    }
    printf("\n\n");
    
    return 0;
    }
    
    
    void abz (char *s, int *res) {
    int i,j,t;
    
    for(j=0; j<SIZ; j++)
      for(i=' ',t = 0 ; i<'~', t<SIZ; i++, t++)
        if(s[j] == i)
         res[t]++;
    
    
    }
    i'm sure there is some better way to do this
    thanks

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    i'm sure there is some better way to do this
    Yes. Only you need to find it

    I would read the input file by characters.
    It is a bit easier to decide what element of res[] should be
    incremented, the array res[] can have size of 256 chars and contain whole ascii table. And then you can index it directly by characters, for example res['a'] is meaningful. Because 'a' is integer value. (maybe not int, but some integer type.)
    And for every '\n' you will increase the lines count.

    Maybe the problem is in condition in the for loop in you function.
    Code:
    for(i=' ',t = 0 ; i<'~', t<SIZ; i++, t++)
    That , operator in condition part seems strange, it should be replaced by something meaningful (&& for example).
    Last edited by aerian; 09-17-2003 at 12:27 PM.

  3. #3
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    thanks for the reply
    yes, i also wanted to read input by character, but then i fugured that if i wanted to count the words in a file fgets would be easyer to use.
    indeed, the problem seems to be in that loop
    just going to write this to read a char at a time then

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define SIZ 127
    
    int main( int argc, char *argv[] )  {
    
    
    int ch,i,temp;
    int total = 0;
    int lines = 0;
    int res[SIZ];
    FILE *fp;
    
    
    for(temp = 0; temp < SIZ; temp++)
    res[temp] = 0;
    
    
    if(argc < 2)
      {
        printf("Include Filename\n");
        exit(EXIT_FAILURE);
      }
    
    if((fp = fopen ( argv[1] , "r")) == NULL)
      {
        fprintf(stderr,"Cannot open file\n");
        exit(EXIT_FAILURE);
      }
    
    while((ch = getc(fp)) != EOF ) {
    
    ++total;
    
    if(ch == '\n')
    ++lines;
    
    for( i = 0; i < SIZ; i++ )
    if(ch == i)
    res[i]++;
    
    
    }
    
    fclose(fp);
    
    printf("\n");
    
    for(temp = 33; temp < 126; temp++ )
     {
    if(temp % 4 == 0)
    printf("\n");
    
    printf("\t.%c. : %d  ",temp,res[temp]);
    }
    
    printf("\n\n\tWhite Spaces : %d  ",res[32]);
    printf("\n\tNew Line Characters : %d  ",lines);
    printf("\n----------------------------------------------------------------\n");
    printf("\nChars total :  %d  ",total);
    
    printf("\n\n");
    
    
    return 0;
    }
    i wrote this using getc function, now i want to count words and an average of chars in a word,how would i do this?

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    First you should decide what is a word and what is not. Is '&*(^*' a word or not? Usually words are separated by whitespace characters, thus ' ', '\t' and '\n'.

    For average number of chars in word you only need to count all characters which can be in word and you will divide it by number of words. To count words you can use one boolean variable to know whether former character was a word-char. Every time you enter a word (so variable in_word is false) you increase number of words. Otherwise you will only update in_word.

    This consumes only constant amount of memory, so it cannot be asymptotically improved. (If you don't know what this means, you may read some tutorial, or use google to find something better.)

    Or you can use combination of standard functions, like fgets(), scanf and fscanf().

  6. #6
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    thanks for helping me a lot

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Code:
      /* notice that this ... */
      for (i = 0; i < SIZ; i++)
        if (ch == i)
          res[i]++;
    
      /* is exactly the same as this ... */
      if ((ch >= 0) && (ch < SIZ))
        res[ch]++;
    
      /* .. only about SIZ times slower */
    also you are counting the number of newlines twice:
    Code:
      /* you could do something like this */
      printf("Number of new lines: %d\n", res['\n']);
    Last edited by DavT; 09-18-2003 at 03:24 AM.
    DavT
    -----------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 08:36 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  3. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 10:40 PM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21