Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

0|Page

Problem No: 02

Problem Name: Write a program to detect comments section of a c program


and strip out in out file.

Algorithm:
Here is the algorithm based on the code, step by step:

1. Start the program.

2. Specify the input C program file name and the output file name without

comments.

3. Use a BufferedReader to read the input file opened for reading.

4. Make use of a BufferedWriter to open the output file for writing.

5. Make the boolean variable inComment's initial value false.

6. Until there are no more lines in the input file, read each line.

7. Go to step 7 if there is a line to read. If not, move on to step 14.

Remove the leading and trailing white spaces by trimming the line.

8. Verify that the line begins with "/*" and that inComment is false.

9. If so, go to step 9. If not, move on to step 11.

10. To process the next line, go to step 6.

11. Verify that the line finishes with "*/" and that inComment is true.

12. If true, skip the current line and set inComment to false. If not, move on to step

13. Verify that the line begins with "//" and that inComment is false.

14. If so, proceed directly to step 6 and skip the line. If not, move on to step 13.

15. Verify the line's emptiness.

16. If so, proceed directly to step 6 and skip the line. If not, move on to step 14.

17. The current line will be written to the output file.

1|Page
18. To process the next line, go to step 6.

19. Snap the input file shut.

20. Snap the output file shut.

21. Print a message of success.

22. End the program.

Code:

2|Page
Fig 1: main code screen short.

3|Page
Output:

Fig 1: output screen short.

4|Page
input.c file screen short

Fig 3: input.c file screen short.

output.txt screen short

Fig 4: output.txt file screen short.

5|Page
Discussion:
In this lab, the provided code offers a basic solution for detecting and stripping out
comments from a C program file. However, there are several potential challenges
and issues that you may encounter when using this code in practice.
The code assumes well-formed comments in the input C program and may not
handle variations such as nested or malformed comments correctly. Additionally,
code snippets resembling comments or inline comments within code lines are not
differentiated from actual comments, potentially leading to incorrect stripping of code.
Comment variations: The code assumes that the input C program has well-formed
comments. However, if the comments depart from the normal format, such as nested
or malformed comments, the code may fail to recognize them. If such differences
occur in your C application, you may need to adjust the code to manage them.
Comment-like code snippets: Code snippets that look like comments but aren't. A
string literal containing characters such as "//" or "/*" is an example. Because the
code does not distinguish between such code snippets and true comments, it may
unintentionally remove code that is not a comment. When executing the code on
systems that may contain such circumstances, you should exercise caution.
Inline comments within code lines: The code currently only processes full-line
comments and ignores single-line comments that begin with "//". If you have inline
comments within code lines in your C program, such as printf("Hello World"); //
Print a message, the code will not handle them appropriately, and the comment
portion of the output may remain. If required, you may need to alter the code to
handle such inline comments.
Preprocessor directives: If your C program contains preprocessor directives like
#include or #define, the code will not distinguish them from ordinary code and may
take them out along with comments. You may need to alter the code to handle
preprocessor directives differently or omit them from the comment stripping process,
depending on your needs.
Issues with file encoding: The code assumes that both the input C program file
and the output file utilize the default file encoding. If your files use a different
encoding, you may experience character encoding difficulties that must be
addressed by providing the right encoding when reading and writing files.
Large file performance: When dealing with exceptionally large C program files, the
code reads and writes the entire file at once, which might have an influence on
memory use and speed. To circumvent memory limits, try processing the file in
smaller segments or using more efficient techniques.
Error handling and exception management: The code offers basic error handling
by catching and displaying IOExceptions using try-catch blocks. However, it may
be advantageous to improve the error handling method in order to offer more
informative error messages or to manage other potential exceptions that may occur
during file operations.

6|Page

You might also like