How do you write a C program which can calculate lines of code but not counting comments?

Showing Answers 1 - 6 of 6 Answers

Geeta.Sanikop

  • Mar 29th, 2006
 

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.

  Was this answer useful?  Yes

SHUBHAM DAS

  • Mar 29th, 2006
 

How do you write a C program which can calculate l...

  Was this answer useful?  Yes

main()

{

FILE f=open a file in readmode

char ch;int c=0;

ch=fgetc(f);

while(!feof(f))

{

switch(ch)

{case "/" :  if (fgetc(f)=="*")    while (fgetc(f)!="*" and fgetc(f)!="/");break;

 case "\n": c=c+1;break;

}

ch=fgetc(f);

close the file;

}

printf(c);

 }

This is a very good question. Here I am presenting my idea.

But I didnot executed it. Could any one execute it and please let me know whether it is working fine or not

venkatesh_ch@fastmail.fm

  Was this answer useful?  Yes

gpreddy

  • Nov 8th, 2006
 

working code ************************#includemain(){ FILE *f; char ch;int c=0; f=fopen("reddy", "r"); ch=fgetc(f); while(!feof(f)) { switch(ch) { case 47 : if (fgetc(f)=="*") while (fgetc(f)!="*" && fgetc(f)!="/") ch=fgetc(f); break; case 10: c=c+1; break; } ch=fgetc(f); } fclose(f); printf("The total number of lines are:%dn", c);}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions