Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Must need to use c language and file system. Work like lexical analyzer. Find out total comment line from a source code will input by me.

What I have tried:

I cant understand how to start...
Posted
Updated 3-Oct-20 3:32am

Strictly speaking C comments start with /* and end with */. All you need to do is:
Open the input file
search for the first /*
output text until you find a */
repeat until end of file

I'll leave the hard work up to you.
Many C compilers also accept // as the start of a comment that extends to the end of the current line, so you may want, or need, to locate those comments, too.
 
Share this answer
 
Comments
Imtiaz Atik 181-15-949 2-Oct-20 14:39pm    
How to search them from file a short code please.
This is a good occasion to learn how a finite state machine works.
Try, for instance, the following code.
It expects its input (C source code with only original C-style (/*...*/) style comments) form stdin (the console), so redirect (using <) the C source file to be processed appropriately).
C
#include <stdio.h>


/* The states defining the state machine */
typedef enum
{
  Idle, /* outside a comment */
  WaitForStar, /* outside a comment, but a '/' has been just processed */
  InComment, /* inside a comment */
  WaitForSlash, /* inside a comment, but a '*' as been just processed */
} State;

/* the state machine implementation */
State process_idle(int );
State process_wait_for_star(int );
State process_in_comment(int );
State process_wait_for_slash(int );

int main()
{
  State state = Idle;
  int i;

  while ( (i = fgetc(stdin)) != EOF )
  {
    switch (state)
    {
    case Idle:
      state = process_idle(i);
      break;
    case WaitForStar:
      state = process_wait_for_star(i);
      break;
    case InComment:
      state = process_in_comment(i);
      break;
    case WaitForSlash:
      state = process_wait_for_slash(i);
      break;
    }
  }

  return 0;
}

State process_idle(int i)
{
  if ( i == '/') return WaitForStar;
  return Idle;
}

State process_wait_for_star(int i)
{
  if ( i == '*' ) return InComment;
  if ( i == '/' ) return WaitForStar;
  return Idle;
}

State process_in_comment(int i)
{
  if ( i == '*') return WaitForSlash;
  putchar(i);
  return InComment;
}

State process_wait_for_slash(int i)
{
  if (i == '/')
  {
    putchar('\n');
    return Idle;
  }
  if (i == '*')
  {
    putchar('*');
    return WaitForSlash;
  }
  putchar('*'); putchar(i);
  return InComment;
}
 
Share this answer
 
v3
Depends on the language you are processing: comments come in many forms, depending on the language they are used in:
//  Single line comment, ends at newline: "modern" C, C++, C#, Java, ...
/* Multiline comment, ends at */ : all C, C++, etc.
' Single line comment, ends at newline: VB
# Single line comment: PHP, Python
C (in column 1): Early Fortran
REM Single line comment, Basic
:: Batch files
; Most assembly languages
-- SQL
*> Cobol

There are probably loads of others, but those are ones I've used!

So decide what constitutes a comment indicator, and parse each line to find them.
 
Share this answer
 
Comments
Imtiaz Atik 181-15-949 2-Oct-20 14:45pm    
I need that for c language please give me a hint code in c by using file methos
OriginalGriff 2-Oct-20 15:03pm    
What have you tried?
Where are you stuck?
What help do you need?
Quote:
How to search them from file a short code please.


We aren't here to do your homework for you: We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900