Thread: Occuring symbol per line

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    1

    Post Occuring symbol per line

    I want to get the occurence of the symbol ';' for each line of this C program. I type the name of the file Source.c and try to count the occuring symbol, but i am getting the value for ALL of the ';' for each line.

    Code:
    
    
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>/* For exit() function */
    
    int main()
    {
       char file_name[150];
       FILE *file2 = 0;
    
       gets(file_name);
    
       {
          int rows = 1;//broq na vsichki redove
    
          int dotcoma[150];
          int j;
          int c=0;
    
          file2 = fopen(file_name, "r");//otvarq faial za chetene
          if (file2 == NULL){
             printf("Cannot open %s\n", file_name);
             exit(2);
          }//if
    
          for (j = 0; j < 150; j++)
             dotcoma[j]=0;
    
          do{
             c = fgetc(file2);
             if (c == '\n') rows++;
    
             if (';' == c)
    
                dotcoma[rows-1] ++;
    
    
    
          } while (c != EOF);//chete do kraq na faila
    
          if (ferror(file2)){
             printf("Error reading file.\n");
    
          }//if
    
          printf("The number of the symbols on a row ");
          printf("Row %d: %f\n", j + 1, (float)dotcoma[j]);
    
       }
    
       if (fclose(file2) == EOF){
          printf("Cannot close %s\n", file_name);
    
       }
       _getche();
       return 0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    When you find a newline (\n) in the file, you know you need to reset the semicolon counter to zero in preparation for the next line.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    8
    Create a function to search the occurrence of ';'. In main create loop to read codes per line

    Read line by line in the main

    Code:
    // main
    char buffer[2048]; // assume that one line of your text is not more than 2048 char.
    
    //open your you want to read and don't forget to close it after.
    
    while(fgets(buffer,sizeof(buffer),fp)){
            cnt=occurance(buffer);
            if(cnt==1) {
                printf("%s",buffer); // this is for DEBUGGING purposes . It will print all the line where the ';' occur
                ret++;
            }
        }
        printf("%d",ret); // display the total ocurance of the char.
    
    //close the file here
    
    // function to search for the occurance of the char.
    
    occurance(char *buffer)
    {
        char found=strchr(buffer,';');
        
        if(found) return 1;
        else return 0;
    }
    it's already 95% of the program .
    Last edited by marque; 12-17-2014 at 08:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem occuring with C++ (OpenGL)
    By luckygirl in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2013, 03:27 AM
  2. Replies: 6
    Last Post: 06-07-2012, 02:50 AM
  3. Replies: 2
    Last Post: 01-10-2012, 08:49 PM
  4. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  5. Replies: 1
    Last Post: 05-20-2006, 11:17 PM

Tags for this Thread