Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

The code return the number of spaces in the buffer using the is space function. So if the input is a b c d then

The code return the number of spaces in the buffer using the is space function. So if the input is a b c d then the output is 4

*/

#include

#include

#include

#include

#include

/*

* Expected usage:

* ./wc

*

* If argv[1] is "words", then you should count the number of words. If it

* is "lines", then you should count the number of lines.

*

* For example:

* $ cat a.txt

* a b c d

* $ ./wc words a.txt

* 4

* $ ./wc lines a.txt

* YOUR PROGRAM MUST COMPILE CLEANLY WITH: gcc -o wc wc.c -Wall -pedantic

*/

/*

* This should return the number of spaces in the buffer using the isspace

* function.

*/

static size_t

words(const char *buffer, size_t length)

{

size_t sum = 0;

return sum;

}

/*

* This should return the number of lines in the buffer by checking for the

* existence of ' ' characters in the buffer.

*/

static size_t

lines(const char *buffer, size_t length)

{

size_t sum = 0;

return sum;

}

int

main(int argc, const char *argv[])

{

char buffer[256];

FILE *fp;

size_t bytes_read;

size_t sum = 0;

enum {

MODE_LINES,

MODE_WORDS

} mode;

{

char buffer[256];

FILE *fp;

size_t bytes_read;

size_t sum = 0;

enum {

MODE_LINES,

MODE_WORDS

} mode;

bool middle = false;

if (argc != 3) {

printf("Usage: ./wc ");

exit(EXIT_FAILURE);

}

/*

* Set mode here based on the value of argv[1]. Use the

* strcmp function for string comparison.

*/

fp = fopen(argv[2], "r");

if (fp == NULL) {

printf("error: could not open %s: %s ",

argv[1], strerror(errno));

exit(EXIT_FAILURE);

}

for (;;) {

bytes_read = fread(buffer, 1, sizeof(buffer), fp);

if (bytes_read == 0)

break;

if (mode == MODE_LINES) {

sum += lines(buffer, bytes_read);

} else if (mode == MODE_WORDS) {

sum += words(buffer, bytes_read);

}

}

}

/*

* You need to increment sum if you were in the middle of a line or wor$

* file was hit.

*/

printf("%zu ", sum + middle);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students explore these related Databases questions