Question

#include <errno.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* * Expected usage:...

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

/*
* Expected usage:
* ./wc <words | lines> <file>
*
* 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
* 1
*
* 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 '\n' 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;
bool middle = false;

if (argc != 3) {
printf("Usage: ./wc <words | lines> <file>\n");
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\n",
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);
}
}
if (argc != 3) {
printf("Usage: ./wc <words | lines> <file>\n");
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\n",
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 word and end of
* file was hit.
*/
printf("%zu\n", sum + middle);
return 0;
}

Please solve the Bolded Questions usong the C Language.

Thanks!
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshots

CODE:

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


/*
* 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;
   int i;
   for(i=0; i<length; i++){
       if(isspace(buffer[i])){
           sum++;
       }
   }
   return sum;

}


/*
* This should return the number of lines in the buffer by checking for the
* existence of '\n' characters in the buffer.
*/
static size_t lines(const char* buffer, size_t length)
{
   size_t sum = 0;
   int i;
   for(i=0; i<length; i++){
       if(buffer[i]=='\n'){
           sum++;
       }
   }
   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;
   bool middle = false;

   if (argc != 3) {
       printf("Usage: ./wc <words | lines> <file>\n");
       exit(EXIT_FAILURE);
   }

   /*
   * Set mode here based on the value of argv[1]. Use the
   * strcmp function for string comparison.
   */

   if(strcmp(argv[1],"words") == 0)
       mode = MODE_WORDS;
   else if(strcmp(argv[1],"lines") == 0)
       mode = MODE_LINES;
   else{
       printf("Usage: ./wc <words | lines> <file>\n");
       exit(EXIT_FAILURE);
   }

   fp = fopen(argv[2], "r");
   if (fp == NULL) {
       printf("error: could not open %s: %s\n",
           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 word and end of
   * file was hit.
   */

   if (feof(fp) == 0){
       middle = true;
   }

   printf("%zu\n", sum + middle);
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
#include <errno.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* * Expected usage:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include...

    devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <termios.h> #include <sys/types.h> #include <sys/mman.h>    #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) #define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1) int main(int argc, char **argv) { int fd; void *map_base = NULL, *virt_addr = NULL; unsigned long read_result, writeval; off_t target; int access_type = 'w';    if(argc...

  • #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc...

    #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc < 4) { fprintf(stderr, "usage: %s <input file 1> <input file 2> <output file>\n", argv[0]); exit(EXIT_FAILURE); } } /* This function takes in the two input file names (stored in argv) and determines the number of integers in each file. If the two files both have N integers, return N, otherwise return -1. If one or both of the files do not exist, it...

  • Following code is a POSIX semaphore usage example. Fill in the blanks. (assume proper headers were...

    Following code is a POSIX semaphore usage example. Fill in the blanks. (assume proper headers were al1 included) int main (int argc, char** argv) sem if (seminit (&sen, 0, 1) == -1) print f ("%s\n", strerror(errno): if printf ("*** Critical Section **n" İS: ( printf("* Non-Critical Section **n") if (semdestroy (&sem) '= 0) printf("%s\n", strerror (errno)); return 0 (&sem) ' 0) printf("%s\n", strerror (errno)); ( & sem) .- 0) printf ("%s\n", strerror (errno) ) ; -

  • My question is listed the below please any help this assignment ; There is a skeleton...

    My question is listed the below please any help this assignment ; There is a skeleton code:  copy_file_01.c #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { char ch ; FILE *source , *target;    if(argc != 3){ printf ("Usage: copy file1 file2"); exit(EXIT_FAILURE); } source = fopen(argv[1], "r"); if (source == NULL) { printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } target = fopen(argv[2], "w"); if (target == NULL) { fclose(source); printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } while ((ch...

  • Below is a basic implementation of the Linux command "cat". This command is used to print...

    Below is a basic implementation of the Linux command "cat". This command is used to print the contents of a file on the console/terminal window. #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) {FILE *fp; if(2 != argc) {priritf ("Usage: cat <filename>\n"); exit(1);} if ((fp = fopen(argv[1], "r")) == NULL) {fprintf (stderr, "Can't. open input file %s\n", argv[1]); exit (1);} char buffer[256]; while (fgets(X, 256, fp) != NULL) fprintf(Y, "%s", buffer); fclose(Z); return 0;} Which one of the following...

  • can someone help me with changing this to c++ language #include <stdlib.h> #include <stdio.h> #include <pthread.h>...

    can someone help me with changing this to c++ language #include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <string.h> #include <dirent.h> #include <sys/wait.h> #include <time.h> #include <sys/stat.h> #include <unistd.h> char *pathLog; char *pathRep;    struct stat attr; int fileNum, curNum; char *create_logPath(char *directory);    void *funcChecker(void *pathSub); void *funcprintTimeAndChanges(void *pathSub);    void main(int argc, char *argv[]) {    if(argc < 3)    { printf("%s must be executed with exactly two additional arguments (pathRep, pathSub)!\n", argv[0]); printf("Typed count is %d\n", argc); printf("Aborted...

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • How to create different files to test whether it passes the serial number checking. (Hint: need...

    How to create different files to test whether it passes the serial number checking. (Hint: need a hex editor.) please provide two files, one pass, one cannot pass C code: ---------------------------------------------- #include <stdio.h> #include <string.h> int chkserial(char *s) { char buffer[16]; strcpy(buffer, s); return strcmp(buffer, "cs780880"); } void fullversion() { printf("Thanks for purchasing! Enjoy the full-version software!\n"); } void trialversion() { printf("This is a trial version. Please purchase the full version to enable all features!\n"); } int main(int argc, char...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT