๐Ÿ’ป
B.TECH I YEAR โ€ข I SEMESTER โ€ข R25

Programming for Problem Solving - Lab Manual

C Programming Laboratory โ€ข Program Solution

๐Ÿ“˜ R25 Regulation ๐ŸŽ“ I Year ๐Ÿ“š I Semester ๐Ÿ’ป C Programming ๐Ÿงช Lab Program
Selected Program

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

โ† All Lab Programs

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

C Program
//to count the lines, words and characters in a given text.
#include <stdio.h>

int main() {
     // declare variables
    char str[200];
    int line, word, ch;

    // initialize count variables with zero
    line = word =1; ch = 0;

    // read multiline string
    printf("Enter string terminated with ~ :\n");
    scanf("%[^~]", str);
   
    for(int i=0; str[i]!='\0'; i++)
    {
        // if it is new line then
        // one line and one word completed
        if(str[i]=='\n')
        {
            line++;
            word++;
        }
        else  if(str[i]==' '||str[i]=='\t')
        {
            word++;
        }
        else
        {
            ch++;
        }
    }
    // display count values
    printf("\nCharacter counts = %d\n", ch);
    printf("Word counts = %d\n", word);
    printf("Line counts = %d\n", line);
    return 0;
}

OUTPUT
Enter string terminated with ~ :
Hello Guest
I am studyglance
A Fully loaded Notebook!~

Character counts = 45
Word counts = 9
Line counts = 3

๐Ÿ’ป Run This C Program

Write a C program to count the lines, words and characters in a given text.
The selected C program is loaded automatically. Edit the code, provide input through STDIN and click Run.

The compiler is embedded using OneCompiler. Your program runs in the embedded C environment; Study Glance does not execute C code directly on your web server.
โ† Back to PPS Lab Programs ๐Ÿ“š All Lab Manuals