0% found this document useful (0 votes)
9 views7 pages

Lab Report 4 (193-15-13375) PDF

Uploaded by

jian.2025
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Lab Report 4 (193-15-13375) PDF

Uploaded by

jian.2025
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Objective:

1. Count the number of lines in a given input text using C.


2. Identify comments (both single-line and multi-line) in the input text.
3. Remove comments (both single-line and multi-line) from the input text.

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.

These will help in understanding:


• The use of input and output functions in C.

• Working with loops and conditionals to process multi-line text.

• Implementing logic to detect and manage single-line (//) and multi-line (/* */) comments in C
code.

Methodology:

1. Count the Number of Lines:


• The first program uses the getchar() function to read each character from the input until the end
of file (EOF) is encountered.

• 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).

2. Identify Comments in the Input:

• 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:

1. Count the Number of Lines:

#include <stdio.h>
int main() {
char c;
int lineCount = 0;

printf("Enter text (Ctrl+D to end input):\n");

while ((c = getchar()) != EOF) {

if (c == '\n') {
lineCount++;

}
}

printf("\nNumber of lines: %d\n", lineCount);

return 0;

2. Identify Comments:

#include <stdio.h>
#include <stdbool.h>

int main() {

char c;
bool inSingleLineComment = false;

bool inMultiLineComment = false;

printf("Enter text (Ctrl+D to end input):\n");

while ((c = getchar()) != EOF) {


if (inSingleLineComment) {

if (c == '\n') {

inSingleLineComment = false;

putchar(c);

} else if (inMultiLineComment) {

if (c == '*' && getchar() == '/') {

inMultiLineComment = false;

} else {

if (c == '/' && getchar() == '/') {

inSingleLineComment = true;

putchar('/'); putchar('/');

} else if (c == '/' && getchar() == '*') {

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;

bool inMultiLineComment = false;

printf("Enter text (Ctrl+D to end input):\n");

while ((c = getchar()) != EOF) {

if (inSingleLineComment) {
if (c == '\n') {

inSingleLineComment = false;
}

} else if (inMultiLineComment) {
if (c == '*' && getchar() == '/') {

inMultiLineComment = false;
}

} else {
if (c == '/' && getchar() == '/') {

inSingleLineComment = true;

} else if (c == '/' && getchar() == '*') {

inMultiLineComment = true;

} else {

putchar(c);

}
}

return 0;

Results:

1. Count the Number of Lines:


• The program successfully counts the number of lines in the input text. For example, if the input
consists of 5 lines of text, the program will output:
Number of lines: 5.

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:

1. Count the number of lines in a given input.

2. Identify and print comments (both single-line and multi-line) in a text.

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/

You might also like