Here in this article we will learn how to identify the comment in the c program and print that comment . In this code we will use the file handling . By using the file handling first we take input by the user and write it in this file and then read the code from the file and find the comment in the code.
Note:- the line start from // is a single line comment in C-programming and the content written in /* comment */ is a multiline comment in C-programming.
So we have to find the both the comments in the code .
To know more about file handling :-- Click here
Code:-
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp;
char str[200];
// logic for writing the code in the file
fp=fopen("file.txt","w");
if(fp==NULL)
printf("Error in opening the file\n");
else
{
char ch;
printf("Enter the $ for ending the editing\n");
scanf("%c",&ch);
fputc(ch,fp);
while(ch!='$')
{
scanf("%c",&ch);
fputc(ch,fp);
}
fclose(fp);
}
printf("The comments are:-\n");
// logic for finding the comment in the code
fp=fopen("file.txt","r");
if(fp==NULL)
printf("Error in opening the file\n");
else
{
while(fgets(str,200,fp)!=NULL)
{
int i,l,flag=0;
l= strlen(str);
for(i=0;i<l-1;i++)
{
// logic to find single line comment
if(str[i]=='/' && str[i+1]=='/')
{
for(int j=i+2;j<l;j++)
printf("%c",str[j]);
printf("\n");
break;
}
// logic to find comment is multiline
if(str[i]=='/' && str[i+1]=='*')
{
i=i+2;
for(int j=i;str[j];j++)
{
// if there is any single line comment inside the multiline
if(str[j]=='/' && str[j+1]=='/')
{
j+=2;
for(int k=j;k<l;k++)
printf("%c",str[k]);
break;
}
else
printf("%c",str[j]);
}
printf("\n");
flag=1;
break;
}
}
// if multiline comment found
// then scan next line for the full comment
if(flag==1)
{
while(fgets(str,200,fp)!=NULL)
{
int i=0;
l=strlen(str);
while(i<l-1)
{
// if there is any single line comment
if(str[i]=='/' && str[i+1]=='/')
{
i+=2;
for(int k=i;k<l;k++)
printf("%c",str[k]);
break;
}
else
{
if(str[i]=='*' && str[i+1]=='/')
{
flag=0;
break;
}
printf("%c",str[i]);
i++;
}
}
printf("\n");
// if comment end then break
if(flag==0)
break;
}
}
}
}
return 0;
}
Output:-
Lets a input of code:-
#include<stdio.h>
int main()
{
/* this is multiline comment
// and this is single line comment inside the multiline comment
and this is end of the multiline comment*/
}
// End of the main function
$
when we give the this input then first this code will written in the file . and then we will read the file and find the comment inside C-program.
Enter the $ for ending the editing
#include<stdio.h>
int main()
{
/* this is multiline comment
// and this is single line comment inside the multiline comment
and this is end of the multiline comment*/
}
// End of the main function
$
The comments are:-
this is multiline comment
and this is single line comment inside the multiline comment
and this is end of the multiline comment
End of the main function
Recommended Post:
Key points:-
- How to set limit in the floating value in python
- What is boolean data type
- How to print any character without using format specifier
- How to check that given number is power of 2 or not
- How to fix limit in double and floating numbers after dot (.) in c++
- How to print a double or floating point number in scientific notation and fixed notation
- How to take input a string in c
- How to reduce the execution time of program in c++.
Cracking the coding interview:-
Array and string:-
Tree and graph:-
Hackerearth Problems:-
- Very Cool numbers | Hacker earth solution
- Vowel Recognition | Hackerearth practice problem solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
Hackerrank Problems:-
- Playing With Characters | Hackerrank practice problem solution
- Sum and Difference of Two Numbers | hackerrank practice problem solution
- Functions in C | hackerrank practice problem solution
- Pointers in C | hackerrank practice problem solution
- Conditional Statements in C | Hackerrank practice problem solution
- For Loop in C | hackerrank practice problem solution
- Sum of Digits of a Five Digit Number | hackerrank practice problem solution
- 1D Arrays in C | hackerrank practice problem solution
- Array Reversal | hackerrank practice problem solution
- Printing Tokens | hackerrank practice problem solution
- Digit Frequency | hackerrank practice problem solution
- Calculate the Nth term | hackerrank practice problem solution
Data structure:-
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
MCQs:-
0 Comments