C Program that Identifies C Type Comments
#include<stdio.h>
void main()
{
char com[50], ch;
int i=2,a=0;
printf("\n Enter C style comment:");
scanf("%[^\n]%*c",com);
printf("%s", com);
if(com[0]=='/')
{
if(com[1]=='/')
printf("\n This is a comment\n");
else if(com[1]=='*')
{
for(i=2;i<=50;i++)
{
if(com[i]=='*' && com[i+1]=='/')
{
printf("\n This is a comment\n");
a=1;
break;
}
else
continue;
}
if(a==0)
printf("\n This is not a comment\n");
}
else
printf("\n This is not a comment\n");
}
else
printf("\n This is not a comment\n");
}
Algorithm:
- Read a line of characters from keyboard
- Check whether the line begins with ‘//’. If so, determine that the given line is a comment
- Check for first two characters. If they are ‘/*’ then check whether there exists '*/' at the end of the line
- Based on the result of the condition check, output whether the given line is a comment or not
Sample Output:
Enter C style comment:
//This is a single line comment
This is a comment
Enter C style comment:
/This is a C Program
This is not a comment
Enter C style comment:
/*Multiline comment begins like this
This is not a comment
Enter C style comment:
/* A Program written in C for Scanning */
This is a comment