[go: up one dir, main page]

0% found this document useful (0 votes)
16 views2 pages

Compiler

This C program counts the total number of words, characters, and lines in a text string entered by the user. It uses integer variables to store the count for each, initially setting them to 0. It then loops through the string, incrementing the appropriate counter variable whenever a space, new line, or other character is encountered. The counts are finally printed at the end.

Uploaded by

21bcs053yashraj
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)
16 views2 pages

Compiler

This C program counts the total number of words, characters, and lines in a text string entered by the user. It uses integer variables to store the count for each, initially setting them to 0. It then loops through the string, incrementing the appropriate counter variable whenever a space, new line, or other character is encountered. The counts are finally printed at the end.

Uploaded by

21bcs053yashraj
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

‭C Program to count Word, Character and Lines in Text File‬

‭INTRODUCTION -‬
‭ his C program is to find and count the total numbers of words, characters and lines in given‬
T
‭string by user. We take different integer input as default value to count them and increase‬
‭their value by one after fetching new word and character.‬

‭Program -‬

‭#include <stdio.h>‬
‭int main()‬
‭{‬
‭char str[100];‬

i‭nt words=0,newline=0,characters=0;‬
‭printf("Enter your string: ");‬
‭scanf("%[^~]",&str);‬

f‭ or(int i=0;str[i]!='\0';i++)‬
‭{‬
‭if(str[i] == ' ')‬
‭{‬
‭words++;‬
‭}‬
‭else if(str[i] == '\n')‬
‭{‬
‭newline++;‬
‭words++;‬
‭}‬
‭else if(str[i] != ' ' && str[i] != '\n'){‬
‭characters++;‬
‭}‬
‭}‬
‭if(characters > 0)‬
‭{‬
‭words++;‬
‭newline++;‬
‭}‬
‭printf("Total number of words : %d\n",words);‬
‭printf("Total number of lines : %d\n",newline);‬
‭printf("Total number of characters : %d\n",characters);‬
‭return 0;‬
‭}‬
‭OUTPUT‬‭-‬

You might also like