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

Write a C program that uses the below getaline function to read in any number of

ID: 3677213 • Letter: W

Question

Write a C program that uses the below getaline function to read in any number of lines from input (up to 1000 characters long for each line). You must keep track of the longest line entered, and once EOF (end of file) is received (via a Ctrl-D) you should stop accepting input. In other words, when the returned length is 0 your input loop should exit and then your program should modify the longest line and replace all instances of vowels in that line with the letter “X”and then display that line on the screen.

Here is an example of how a run of your program may look: NOTE: Bold text represents input that someone will type into your program.

You should use #define to declare a variable to hold the maximum number of characters that are allowed to be entered on each line. You'll need to keep track of the longest line length and the contents of the longest line. After your input loop has finished you then need to go through the array of characters in the longest line and replace all vowels with the letter “X”, then display the modified line. You do not need to modify the getaline function in any way, however you may do so if you feel it will help you.

Given Code:

getaline function prototype:

int getaline(char [], int);

getaline function:

/* getaline: read a line into char array s up to lim characters, return length */

int getaline(char s[], int lim)

{

   int c, i, j;

   j = 0;

   for (i = 0; (c = getchar()) != EOF && c != ' '; ++i)

    {

       if (i < lim - 2)

       {

          s[j] = c;

          j++;

        }

     }

    if (c == ' ')

   {

        s[j] = c;

      j++;

      i++;

   }

   s[j] = '';

   return i;

}

Hello There This is a test This is a really long line And this is short (User presses Ctrl-D to send EOF Modified Longest Line: ThXs Xs X rXXlly 1Xng 1XnX

Explanation / Answer

#include <stdio.h>
// max line length
#define MAXLINE 1000

int getaline();
void copy();
void printer();

int main()
{
// initalize variables
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
printf("[User presses Ctrl-D to send EOF] ");

// length of longest line stored here
max = 0;

// while the length of the line is greater than 0

while ((len = getaline(line, MAXLINE)) > 0)

if (len > max){

max = len;

// copy the line
copy (longest, line);

}

if (max > 0)
printer(longest);
printf("Modified Longest Line: %s", longest);

return 0;

}

// This function prints the longest string with X's for vowels

void printer(char to[]){

int i;

i = 0;

// while not at the last element of array

while (to[i] != '')
// check if current array element is a vowel
switch (to[i]){
// if it is a vowel

case 'a': case'i': case'e': case'o': case 'u':
case 'A': case 'I': case'E': case 'O': case 'U':
// replace the value with 'X'
to[i] = 'X';

i++;
break;

// If the element is a constant, break

default:
i++;
break;
}

}

//getaline: read a line into s, return length
int getaline(char s[], int lim)

{
int c, i;
for (i=0; i < lim-1 && (c=getchar()) != EOF && c!=' '; ++i)
s[i] = c;
if (c == ' '){
s[i] = c;
++i;

}
s[i] = '';
return i;

}

// copy 'from' into 'to'; assuming to is big enough

void copy(char to[], char from[])
{

int i;
i = 0;
// while not last element, copy the element to to from

while ((to[i] = from[i]) != '')

++i;

}

sample output

[User presses Ctrl-D to send EOF]                                                                                                                           
This is really a long line Modified Longest Line: ThXs Xs rXXlly X lXng lXnX sh-4.3$                                                                        

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote