Mark A. Holmes

CSE 5700, Spring 2023

Lab 1

February 26, 2023

 

 

 

LAB ASSIGNMENT #1

Problem Statement:

Write a C Program to Scan and Count the number of characters, words, and lines in a piece of text input (or a text file). You should use the free C compiler option that I provided or you may use any other C compiler platform.

AIM :

To Write a C Program to Scan and Count the number of characters, words, and lines in a file.

ALGORITHM / PROCEDURE/PROGRAM:

1. Start

2. Read the input file/text

3. Initialize the counters for characters, words, lines to zero

4. Scan the characters, words, lines and

5. increment the respective counters

6. Display the counts

7. End

Input: Enter the Identifier input string below. (You can create an input file and read the file if you want) :

These are a few words for my C programming                                                                                                                                          assignment. My name is <your name>                                                                                                                                                         and I love Computer Science.

Output:

Number of characters:  <program should produce it>

Number of words:  <program should produce it>

Number of lines: <program should produce it>

 

Submission: Please copy both your source code put them in a word file which you should upload. Make sure it is a word file because I will need to run it.

 

 

 

 

The file:

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

    FILE * file;

    char path[100];

{

    FILE * file;

    char path[100];

 

    char ch;

    int characters, words, lines;

 

 

    /* Input path of files to merge to third file */

    printf("Enter source file path: ");

    scanf("%s", path);

 

    /* Open source files in 'r' mode */

    file = fopen(path, "r");

 

 

    /* Check if the file opened successfully */

    if (file == NULL)

    {

        printf("\nUnable to open file.\n");

        printf("Please check if the file exists and you have read privilege.\n");

 

        exit(EXIT_FAILURE);

    }

 

    /*

     * Logic for counting characters, words and lines.

     */

    characters = words = lines = 0;

    while ((ch = fgetc(file)) != EOF)

    {

        characters++;

 

        /* Check new line */

        if (ch == '\n' || ch == '\0')

            lines++;

 

        /* Check words */

        if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')

            words++;

    }

 

    /* Increment words and lines for last word */

    if (characters > 0)

    {

        words++;

        lines++;

    }

 

    /* Print file statistics */

    printf("\n");

    printf("Total characters = %d\n", characters);

    printf("Total words      = %d\n", words);

    printf("Total lines      = %d\n", lines);

 

 

    /* Close files to release resources */

    fclose(file);

 

    return 0;

}

 

    char ch;

    int characters, words, lines;

 

 

    /* Input path of files to merge to third file */

    printf("Enter source file path: ");

    scanf("%s", path);

 

    /* Open source files in 'r' mode */

    file = fopen(path, "r");

 

 

    /* Check if the file opened successfully */

    if (file == NULL)

    {

        printf("\nCan’t open file.\n");

        printf("Please check if the file exists and you have read privilege for it.\n");

 

        exit(EXIT_FAILURE);

    }

 

    /*

     * Logic to count characters, words and lines.

     */

    characters = words = lines = 0;

    while ((ch = fgetc(file)) != EOF)

    {

        characters++;

 

        /* Check new line */

        if (ch == '\n' || ch == '\0')

            lines++;

 

        /* Check words */

        if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')

            words++;

    }

 

    /* Increment words and lines for last word */

    if (characters > 0)

    {

        words++;

        lines++;

    }

 

    /* Print file statistics */

    printf("\n");

    printf("Total characters = %d\n", characters);

    printf("Total words      = %d\n", words);

    printf("Total lines      = %d\n", lines);

 

 

    /* Close files to release resources */

    fclose(file);

 

    return 0;

}

Go Back