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

Lab Program

The document describes a C program to count the lines, words, and characters in a given text. The program prompts the user to enter lines of text with "END" to finish. It then prints the number of characters, words, and lines. The source code for the C program is provided. Sample test case input and output is also shown.

Uploaded by

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

Lab Program

The document describes a C program to count the lines, words, and characters in a given text. The program prompts the user to enter lines of text with "END" to finish. It then prints the number of characters, words, and lines. The source code for the C program is provided. Sample test case input and output is also shown.

Uploaded by

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

12/9/2019 https://rgmcet.codetantra.com/secure/labs-q.jsp?

sNo=34&qId=5b9e68c018b23e0ba62a5e4e&bd=AY3RFZHVEQg%3D%3D&lid=5d…

Exp. Name: Write a C program to Count the Lines, Words and Characters in a given
S.No: 34 Date:
text

Page No:
Aim:
Write a C program to count the lines, words and characters in a given text.

ID: 19091A04P2
At the time of execution, the program should print the following message on the console as:

Enter lines of text (enter END to complete)

For example, if the user gives the input as:

Enter lines of text (enter END to complete)


CodeTantra developed a Robotic tool
In the year 2016
END

then the program should print the result as:

Character = 51, Words = 9, Lines = 2

Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:

CountLinesWords.c

#include<stdio.h>
#include<string.h>
void main()
{
char str[70];
int Character=0,Words=0,Lines=0,i;

Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous)


printf("Enter lines of text (enter END to complete)\n");
while(1)
{
gets(str);
if(strcmp(str,"END")==0)break;
Lines++;
Words=Words+1;
for(i=0;str[i]!='\0';i++)
{
if(str[i]== ' ' ||str[i]=='\t') Words++;
Character++;
}
}
printf("Character = %d, Words = %d, Lines = %d\n",Character,Words,Lines);
}

Execution Results - All test cases have succeeded!

Test Case - 1

User Output

https://rgmcet.codetantra.com/secure/labs-q.jsp?sNo=34&qId=5b9e68c018b23e0ba62a5e4e&bd=AY3RFZHVEQg%3D%3D&lid=5d7fba0de9d70… 1/2
12/9/2019 https://rgmcet.codetantra.com/secure/labs-q.jsp?sNo=34&qId=5b9e68c018b23e0ba62a5e4e&bd=AY3RFZHVEQg%3D%3D&lid=5d…

Test Case - 1
Enter lines of text (enter END to complete) CodeTantra developed a Robotic tool
in the year 2016 in the

Page No:
year
2016
and they released in 2017 and

ID: 19091A04P2
they
released
in 2017
END END
Character = 76, Words = 14, Lines = 3

Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous)

https://rgmcet.codetantra.com/secure/labs-q.jsp?sNo=34&qId=5b9e68c018b23e0ba62a5e4e&bd=AY3RFZHVEQg%3D%3D&lid=5d7fba0de9d70… 2/2

You might also like