Thread: Finding the blank lines and theline with minimum keywords in a .C file

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    1

    Finding the blank lines and theline with minimum keywords in a .C file

    Hi I'm trying to build a program in C that scans a .c file and shows how many blank lines are in there and what is the number of the line with minimum keywords finds. By keywords I mean - auto, break, case, char, const, continue, default, do and etc. I've already managed to find the blank lines but I have no idea how to find the number of the line with minimum keywords.. Any ideas and suggestions?
    This is the code so far:
    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    #include <stdlib.h>
    
    
    
    
    int getEmptyLines(const char *fileName)
    {
        FILE *fp;
        int emptyLine = 0;
        char line[300];
        char fnamer[100] = "";        //Storing File Path/Name of Image to Display
        printf("\n\nPlease Enter the Full Path of the Image file you want to view: \n");
        scanf("%s", &fnamer);
        fp = fopen(fnamer, "r");
        if (fp == NULL)
        {
            printf("\n%s\" File NOT FOUND!", fnamer);
            _getch();
            exit(1);
        }
        else {
            while (fgets(line, 300, fp)) {
                int i = 0;
                int len = strlen(line);
                emptyLine++;
                for (i = 0; i < len; i++) {
                    if (line[i] != '\n' && line[i] != '\t' && line[i] != ' ') {
                        emptyLine--;
                        break;
                    }
                }
            }
            return emptyLine;
        }
    }
    
    
    int main(void)
    {
        const char fileName[] = "text.txt";
        int emptyLines = getEmptyLines(fileName);
        if (emptyLines >= 0) {
            printf("The number of empty lines is %d", emptyLines);
        }
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well what you need to do is break the input lines into words you can use say strcmp on.

    Code:
    #include <stdio.h>
    #include <stddef.h>
    #include <stdlib.h>
    
    int main()
    {
      char buff[BUFSIZ];
      while ( fgets(buff,BUFSIZ,stdin) ) {
        char token[BUFSIZ];
        char *p = buff;
        while ( *p != '\0' ) {
          int pos;
          if ( sscanf(p,"%[A-Za-z]%n",token,&pos) == 1 ) {
            printf("Word=>>%s<< of length %d\n",token,pos);
            p += pos;
          }
          if ( sscanf(p,"%[^A-Za-z]%n",token,&pos) == 1 ) {
            printf("Non-Word=>>%s<< of length %d\n",token,pos);
            p += pos;
          }
        }
      }
      return 0;
    }
    
    Example run:
    $ ./a.out 
    int main()
    Word=>>int<< of length 3
    Non-Word=>> << of length 1
    Word=>>main<< of length 4
    Non-Word=>>()
    << of length 3
      char buff[BUFSIZ];
    Non-Word=>>  << of length 2
    Word=>>char<< of length 4
    Non-Word=>> << of length 1
    Word=>>buff<< of length 4
    Non-Word=>>[<< of length 1
    Word=>>BUFSIZ<< of length 6
    Non-Word=>>];
    << of length 3
      while ( fgets(buff,BUFSIZ,stdin) ) {
    Non-Word=>>  << of length 2
    Word=>>while<< of length 5
    Non-Word=>> ( << of length 3
    Word=>>fgets<< of length 5
    Non-Word=>>(<< of length 1
    Word=>>buff<< of length 4
    Non-Word=>>,<< of length 1
    Word=>>BUFSIZ<< of length 6
    Non-Word=>>,<< of length 1
    Word=>>stdin<< of length 5
    Non-Word=>>) ) {
    << of length 6

    But you have to be careful, what answer would this code produce when you analyse it?
    Code:
    if ( strcmp(token,"if") == 0 ) 
        printf("We found if\n"); // we found an if statement.
    A simple interpretation would locate the word 'if' four times.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .txt file across (blank) lines into struct
    By sean_cantab in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2016, 05:30 AM
  2. finding lines of text from one file in another
    By Zimbobo in forum C Programming
    Replies: 4
    Last Post: 11-21-2009, 10:06 PM
  3. Replies: 2
    Last Post: 05-28-2009, 09:58 PM
  4. Replies: 3
    Last Post: 06-25-2003, 04:29 PM

Tags for this Thread