Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Ch.11 PE 1 (ENHANCED) Write a function that fetches the next n chars from the us

ID: 3767278 • Letter: C

Question

Ch.11 PE 1 (ENHANCED) Write a function that fetches the next n chars from the user input, including blanks, tabs, and newlines (hint: use getchar()), and stores them in an array passed to the function. For example, void fetchn(char *a, int n); Test it with a program that prompts the user for the number of chars to read, and then for the characters. The test program should call fetchn() and print out the array contents. For example (program output shown underlined): How many characters should I read? 7 Start entering characters, and I will quit taking them at 7: he Ll o The first 7 characters you typed were: he Ll o

Explanation / Answer

Program:

Sample output:

Code to copy:

#include <stdio.h>

#define max 200

void fetchn(char *, int );

int main()

{

     char str[200];

     int noChars;

     int i=0;

     char ch;

     //promt the user to enter number of chars to read and chars

     printf("How many characters should I read? ");

     scanf("%d",&noChars);

     printf("Start entering characters, and I will quit taking them at 7:");

     //read charcters tabs newline etc...

     while(i<=noChars)

     {

          str[i]=getchar();

          i++;

     }

     //call fetchn()

     fetchn(str,noChars);

     return 0;

}

//fetches n characters from the array a

void fetchn(char *a, int n)

{

     printf("The first %d characters you typed were:", n);

     //fetches and prints n characters

     for(int i=0;i<=n;i++)

     {

          printf("%c",*(a+i));

     }

}