Question

Hi everyone, I have a C programming problem, answers are better with explanations.

Background Materials:

In most of the C code you have looked at so far in this course, input with scanf has read multiple characters per function call, and output with printf has written multiple characters per function call However, a central idea behind the design of the I/O (short for input/output) functions in the Standard C library is that input and output of text is really done one byte at a time. This is true for terminal input and output streams, and also true for input and output streams related to files In this exercise, wel look at the fputc (single character output) and fgeto (single character input) functions Before explaining how fputc and fgetc work, I have to say a few words about FILE stdin, stdout, and EOF, which are all facilities offered by the <stdio.h> header file FILE is a type, related to handling files, as you may have guessed. For this lab, if an input function has an argument of type FILE *, use stdin to get input if an output function has an argument of type FILE * use stdout to send all you need to know about FILE is this: from the terminal:; output to the terminal

The Task:

The Answer:

The completed C code.

Below is the file charIO4C.c:

#include <stdio.h>
#include <ctype.h>

#define QUIT_LETTER 'q'

int main(void)
{
  int c, line_length;

  // Each pass through the outer loop reads a line of input.
  while (1) {
    printf("\nEnter a line of text. (To quit, start the line with %c.)\n",
           QUIT_LETTER);
    c = fgetc(stdin);
    if (c == EOF || c == QUIT_LETTER)
      break;                    // Leave outer loop.

    line_length = 0;
    printf("Changing uppercase to lowercase ...\n");

    // Inner loop reads chars until '\n' or EOF is found.
    while (c != '\n' && c != EOF) {
      line_length++;
      fputc(tolower(c), stdout);
      c = fgetc(stdin);
    }
    fputc('\n', stdout);
    printf("Length of line, not counting '\\n', was %d.\n", line_length);
  }

  printf("Bye!\n");
  return 0;
}

Thanks a lot!

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

//Modified program will print number of alphabets in addition to the number of characters read

#include <stdio.h>

#include <ctype.h>

#define QUIT_LETTER 'q'

int main(void)

{

int c, line_length, tot_alpha;

// Each pass through the outer loop reads a line of input.

while (1) {

    printf("\nEnter a line of text. (To quit, start the line with %c.)\n",

           QUIT_LETTER);

    c = fgetc(stdin);

    if (c == EOF || c == QUIT_LETTER)

      break;                    // Leave outer loop.

    line_length = 0;

    tot_alpha = 0;     //variables to hold number of alpabets

    printf("Changing uppercase to lowercase ...\n");

    // Inner loop reads chars until '\n' or EOF is found.

    while (c != '\n' && c != EOF) {

      line_length++;

      if(isalpha(c)!=0) //Checking for alphabets

                tot_alpha++;

      fputc(tolower(c), stdout);

      c = fgetc(stdin);

    }

    fputc('\n', stdout);

    printf("Length of line, not counting '\\n', was %d.\n", line_length);

    //Printing total alphabets

    printf("%d of the characters in the line were letters\n", tot_alpha);

}

printf("Bye!\n");

return 0;

}

//Modified program will print number of digits in addition to the number of characters read

#include <stdio.h>

#include <ctype.h>

#define QUIT_LETTER 'q'

int main(void)

{

int c, line_length, tot_digits;

// Each pass through the outer loop reads a line of input.

while (1) {

printf("\nEnter a line of text. (To quit, start the line with %c.)\n",

QUIT_LETTER);

c = fgetc(stdin);

if (c == EOF || c == QUIT_LETTER)

break; // Leave outer loop.

line_length = 0;

tot_digits = 0; //variables to hold number of digits

printf("Changing uppercase to lowercase ...\n");

// Inner loop reads chars until '\n' or EOF is found.

while (c != '\n' && c != EOF) {

line_length++;

if(isdigit(c)!=0) //Checking for digit

tot_digits++;

fputc(tolower(c), stdout);

c = fgetc(stdin);

}

fputc('\n', stdout);

printf("Length of line, not counting '\\n', was %d.\n", line_length);

//Printing total digits

printf("%d of the characters in the line were digits\n", tot_digits);

}

printf("Bye!\n");

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Hi everyone, I have a C programming problem, answers are better with explanations. Background Materials: The...
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
  • 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...

  • Hi everyone, I have a problem about C programming. Background Material: The figure 6 is as...

    Hi everyone, I have a problem about C programming. Background Material: The figure 6 is as below: The task: Download the file 55.c. Study the C code, then build an executable and run it to see what the output is. Modify the program so that the output is The string in buffer is "In C the value of 12 + 34 / 5 is 18." Do this by looking up decimal values for ASCII codes in Figure 6 and typing...

  • Write a C Program that uses file handling operations of C language to copy the contents...

    Write a C Program that uses file handling operations of C language to copy the contents of a file called “original.dat” to another file called “copy2.dat”. Here, you will read and write from/to the files line by line with the help of fgets() and fputs() functions. Sample file “original.dat” has been provided. Please note that the new file “copy2.dat” should have exactly same contents as in “original.dat”. #include <stdio.h> int main() { FILE *fptr1, *fptr2; if((fptr1=fopen("original.dat","r"))==NULL) printf("\n Error opening File");...

  • 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...

  • Edit a C program based on the surface code(which is after the question's instruction.) that will...

    Edit a C program based on the surface code(which is after the question's instruction.) that will implement a customer waiting list that might be used by a restaurant. Use the base code to finish the project. When people want to be seated in the restaurant, they give their name and group size to the host/hostess and then wait until those in front of them have been seated. The program must use a linked list to implement the queue-like data structure....

  • Counting characters, words, and lines based on a text file

    I did the assigment but inside mainWrite a C program (called counting.c) that counts the number of characters, words and lines readfrom standard input (stdin) until EOF is reached. This means counting.c must contain a mainfunction along with other function.- Assume the input is ASCII text of any length.-Every byte read from stdin counts as a character except EOF.- Words are defined as contiguous sequences of letters (a through z, A through Z) and the apostrophe ( ' which has...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • The program is written in c. How to implement the following code without using printf basically...

    The program is written in c. How to implement the following code without using printf basically without stdio library? You are NOT allowed to use any functions available in <stdio.h> . This means you cannot use printf() to produce output. (For example: to print output in the terminal, you will need to write to standard output directly, using appropriate file system calls.) 1. Opens a file named logfle.txt in the current working directory. 2. Outputs (to standard output usually the...

  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

  • Write a c++ program in that file to perform a “Search and Replace All” operation. This...

    Write a c++ program in that file to perform a “Search and Replace All” operation. This operation consists of performing a case-sensitive search for all occurrences of an arbitrary sequence of characters within a file and substituting another arbitrary sequence in place of them. Please note: One of the biggest problems some students have with this exercise occurs simply because they don’t read the command line information in the course document titled “Using the Compiler’s IDE”. Your program: 1. must...

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
ADVERTISEMENT