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