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

Your programs must include the following information in comments: (1) problem nu

ID: 3808779 • Letter: Y

Question

Your programs must include the following information in comments: (1) problem number, (2) course information, (3) assignment, (4) date, and (5) student name. 1. (20 points) Modify the given read line function each of the following ways: a. Have it skip white space before beginning to store input characters. Name this function read line a b. Have it stop reading at the first white space character. Name this function read line b. c. Have it stop reading at the first new-line character, then store the new-line character in the string. Name this function read line c. d. Have it leave behind the characters that it doesn't have room to store. Do not read these from input. Name this function read line d. You will submit a program that calls each ofthe four read line functions. Your program should call each of the functions from main and display the resulting strings. Use these global constants for the whitespace characters: Global Constants const int horizontalTab 9; const int linefeed 10; const int verticalTab 11; const int space 32; Ifn int read line (char str0, int n) brief reads characters from standard input and stores them in str. *param[in] str character array "Vparam[in] n ength of str array * eturn number of characters stored in str

Explanation / Answer


int read_line_a(int str[],int n){
   int ch,i=0;
   int isFirstSpaceChar=1;//this flag will become 0 when it encounters a first non-space character
   while(ch=getchar())!=' '){//checking till we encounter a ' '.here we will read all characters form input
       if(isFirstSpaceChar==1 && 9!=(int)ch && 10!=(int)ch && 11!=(int)ch && 32!=(int)ch)//checking for occurence of first non-space character.
       isFirstSpaceChar=0;//if such non-space char found, we will set isFirstSpaceChar to 0;
      
       if(isFirstSpaceChar==0 && i<n){// we will store a character only if that flag is 0 and if str array has space to store that char.
           str[i++]=ch;
       }
   }
   if(i<n)//if we have enough space, we will store null terminator
   str[i]='';
  
   return i;//returns number of characters stored in str
}
int read_line_b(int str[],int n){
   int ch,i=0;
   //loop until we found a white space char or ' '
   while(ch=getchar())!=' ' && 9!=(int)ch && 10!=(int)ch && 11!=(int)ch && 32!=(int)ch){

       if(i<n){
           str[i++]=ch;
       }
   }
   if(i<n)//if we have enough space, we will store null terminator
   str[i]='';
  
   return i;//returns number of characters stored in str
}
int read_line_c(int str[],int n){
   int ch,i=0;
   while(ch=getchar())!=' '){//loop until we found ' '.here we will read all characters form input
       //we are ensuring that to have a space to store a ' ' char atlast.
       if(i<n-1){
           str[i++]=ch;
       }
   }
   str[i++]=' ';//assigning ' '
  
   if(i<n)//if we have enough space, we will store null terminator
   str[i]='';
  
   return i;//returns number of characters stored in str
}
int read_line_d(int str[],int n){
   int ch,i=0;
   while(i<n && ch=getchar())!=' '){//loop until we found ' ' or if there is no space in str to store characters.Here we will read atmost n characters from input

       if(i<n){
           str[i++]=ch;
       }
   }
  
   if(i<n)//if we have enough space, we will store null terminator
   str[i]='';
  
   return i;//returns number of characters stored in str
}