CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
How do you write a C program which can calculate lines of code but not counting comments?


Category C Interview Questions
Rating (0.0) By 0 users
Added on 7/18/2006
Views 1356
Rate it!
Answers:

Using file concept with Command line arguments.declare a variable (lcnt) used to count the no of lines.Open a file in read made and then using while loop check the condition for not equal to EOF.Later using if condition check check for new line and increment the variable for counting the lines.

Then using while,check for the character '/','*' (as the comments start with these characters) and end with ('*' and '/').if condition of this is true then break and come out of the block else increment the line.



Here is the code for both single comment and multiple comments(Here multiple newline characters are not taken care of)
#include<stdio.h>
int main()
{
FILE *fp;
int nol=0;//To count the number of lines
int comment=0;//As a flag to indicate comment is started
char ch;
fp=fopen("file.txt","r");//here file.txt is a file which i created with some lines,comments both // and /* ...*/
if(fp==NULL)
{
printf("
Cannot open the file ");
getch();
return;
}

while(!feof(fp))
{
ch=getc(fp);
if(ch == '
' && comment == 0)
nol++; //increment only when a newline charecter occurs outside the comment

if(ch == '/'){
ch = getc(fp);
if(ch=='*')//check for starting comment /* (multiple lines comment)
comment=1;
else
if(ch=='/')//check for starting comment // (single line comment)
while((ch = getc(fp)) != '
')
;
}
if(ch == '*'){
ch = getc(fp);
if(ch=='/' )
comment=0;
if((ch =getc(fp) =='
'))
;
}

}

fclose(fp);

printf("
Number of lines = %d",nol);
getch();
}



 Posted by: Syed Baseer Ahmed    

Contact Syed Baseer Ahmed  Contact Syed Baseer Ahmed


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
How to swap the content of two variables without a temporary variable

View Answer
Output of this Programme please??

main()
{

int a[]={2,4,6,8,10};
int i;
change(a,5);
for(int i=0;i<=4;i++)
printf(" %d", a[i]);
}

change(int *b,int n)
{

int i;
for(i=0;i *(b+i) = *(b+i)+5;
}

sytaxis correct?? was asked i a test


View Answer
Can we use string in switch statement?

View Answer
How can you calculate number of nodes in a circular Linked List?

View Answer
What is a NULL Macro? What is the difference between a NULL Pointer and a NULL Macro?

View Answer
Where are the auto variables stored? What is the use of register variables?

View Answer
c program for delete a node from linked list


View Answer
What is the differece between #define and constant in C?

View Answer
How can we open a image file through C program

View Answer
In c , main() is a function . and where is defined main() in c. bcz every function has three parts.
1>. decleration
2>. definition.
3>. calling

View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/5961/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

1.77