Lab Report 4 (193-15-13375) PDF
Lab Report 4 (193-15-13375) PDF
Introduction:
In this lab, we are tasked with writing C programs to perform three functions:
1. Count the number of lines in a given input text.
2. Identify comments (both single-line and multi-line comments) in the input text.
3. Remove comments (both single-line and multi-line comments) from the input text.
• Implementing logic to detect and manage single-line (//) and multi-line (/* */) comments in C
code.
Methodology:
• The program increments a counter each time it encounters a newline character (\n), indicating
the end of a line.
• The program continues reading input until the EOF marker is encountered (typically Ctrl+D on
Unix/Linux or Ctrl+Z on Windows).
• This program detects and prints both single-line and multi-line comments in the input text.
• The logic is based on flags (inSingleLineComment and inMultiLineComment):
o Single-line comment (//): The program sets a flag when it encounters // and continues
printing characters until a newline is reached.
o Multi-line comment (/* */): The program sets another flag when it encounters /* and
prints characters until it finds the */ that closes the block.
3.Remove Comments:
• This program uses the same approach as the previous one, but instead of printing the
comments, it simply skips over the comment characters.
When it encounters a single-line comment (//), the program ignores all characters until a
•
newline is found.
When it encounters a multi-line comment (/*), the program ignores all characters until it finds
• the closing */.
Programs:
#include <stdio.h>
int main() {
char c;
int lineCount = 0;
if (c == '\n') {
lineCount++;
}
}
return 0;
2. Identify Comments:
#include <stdio.h>
#include <stdbool.h>
int main() {
char c;
bool inSingleLineComment = false;
if (c == '\n') {
inSingleLineComment = false;
putchar(c);
} else if (inMultiLineComment) {
inMultiLineComment = false;
} else {
inSingleLineComment = true;
putchar('/'); putchar('/');
inMultiLineComment = true;
putchar('/');
putchar('*');
} else {
putchar(c);
return 0;
}
3. Remove Comments:
#include <stdio.h>
#include <stdbool.h>
int main() {
char c;
bool inSingleLineComment = false;
if (inSingleLineComment) {
if (c == '\n') {
inSingleLineComment = false;
}
} else if (inMultiLineComment) {
if (c == '*' && getchar() == '/') {
inMultiLineComment = false;
}
} else {
if (c == '/' && getchar() == '/') {
inSingleLineComment = true;
inMultiLineComment = true;
} else {
putchar(c);
}
}
return 0;
Results:
2. Identify Comments:
• The program correctly identifies and outputs single-line and multi-line comments. For example,
inputting a piece of code with comments will print the comments in the output.
3. Remove Comments:
• The program removes both single-line and multi-line comments from the input text, leaving only
the non-comment code. For example, inputting code with both types of comments will result in
the removal of those comments.
Conclusion:
The C programs successfully perform the following tasks:
3. Remove comments from the text while printing the remaining code.
These exercises demonstrated the use of loops, conditionals, and handling of various input/output
operations in C. The manipulation of comment blocks using flags and the get char() function helped in
understanding how to process and modify source code text. This lab has reinforced fundamental C
programming concepts and provided a practical application of handling different types of input data.
References:
• C Programming Language by Brian W. Kernighan and Dennis M. Ritchie
• C Standard Library Documentation - https://www.cplusplus.com/reference/