0% found this document useful (0 votes)
18 views

Program 2

Uploaded by

enashusingh001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Program 2

Uploaded by

enashusingh001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1. Write a C program to identify whether a given lines is comment or not.

2. Write a C program to count the characters, words and lines in a file

1. Write a C program to identify whether a given line is a comment or not.


PROGRAM LOGIC:
a. Read the input string.
b. Check whether the string is starting with ‘/’ and check next
character is ‘/’ or’*’.
c. If condition satisfies print comment.
d. Else not a comment

2.1 PROGRAM:
#include<stdio.h> #include<conio.h> void main() {
char com[30]; int i=2,a=0; clrscr();
printf("\n Enter comment:"); gets(com);
if(com[0]=='/') {
if(com[1]=='/')
printf("\n It is a comment"); else if(com[1]=='*') {
for(i=2;i<=30;i++)
{
if(com[i]=='*'&&com[i+1]=='/')
{
printf("\n It is a comment"); a=1;
break; }
} else continue; }
else if(a==0)
printf("\n It is not a comment");
printf("\n It is not a comment");
}
else
printf("\n It is not a comment"); getch(); }
2.2 INPUT & OUTPUT:
Input: Enter comment: //hello
Output: It is a comment
Input: Enter comment: hello
Output: It is not a comment

// C Program to count

// the Number of Characters in a Text File

#include <stdio.h>

#define MAX_FILE_NAME 100

int main()

FILE* fp;

// Character counter (result)

int count = 0;

char filename[MAX_FILE_NAME];

// To store a character read from file

char c;

// Get file name from user.

// The file should be either in current folder

// or complete path should be provided

printf("Enter file name: ");


scanf("%s", filename);

// Open the file

fp = fopen(filename, "r");

// Check if file exists

if (fp == NULL) {

printf("Could not open file %s",

filename);

return 0;

// Extract characters from file

// and store in character c

for (c = getc(fp); c != EOF; c = getc(fp))

// Increment count for this character

count = count + 1;

// Close the file

fclose(fp);

// Print the count of characters

printf("The file %s has %d characters\n ",

filename, count);

return 0;

You might also like