Question

Simplify for loop condition in getline.

This program reads lines from the terminal, converts them to C strings by appending \0, and prints the longest one found. We need the ability to read in a line and the ability to save a line. We write two function getline and copy for these task.

Using C language

#include <stdio.h> #def i ne MAXL INE 1 000 int getL ine(char ne], int maxl ine); void copy( char to[], char from]); int main) int len, max; char Tine[MAXL INE], longest [MAXL INE] max0; whi le ((len getL ine(ine, MAXLINE))>0) if (len > max) max = len; copy (longest,line) if (max>0) printf(%s, longest); return 0; int getL ine(char s[], int lim) int c, i; for (i-0; i<lim-1 && (c=getchar()) !=EOF && s[i] = c; if (c=th) { return i; void copy( char to[], char from]) int i; i=0 ; while ((to[i] = from[i]) !-WO)

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

It can't be simplify. In that loop, 3 conditions,

1) length condition

2) checking end of file(Ctrl+D)

3) checking for end of line

This 3 conditions are mandatory. we can't simply that conditions.

Let me know if you have any clarifications. Thank you...

Add a comment
Know the answer?
Add Answer to:
Simplify for loop condition in getline. This program reads lines from the terminal, converts them to...
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
  • Create a UNIX makefile for the following C program: //convert.c char toUpper(char c){ if(c>='a' && c<='z')...

    Create a UNIX makefile for the following C program: //convert.c char toUpper(char c){ if(c>='a' && c<='z') return c-32; return c; } //converting sentance to upper void convertSentence(char *sentence){ int i=0; while(sentence[i]!='\0'){ //convert to upper for each character sentence[i] = toUpper(sentence[i]); i++; } } //converting all sentences into uppercase void convertAll(char **sentenceList, int numOfSentences){ int i=0; while(i<numOfSentences){ //calling convertsentence function to conver uppercase convertSentence(sentenceList[i]); i++; } } sentences.c #include<stdio.h> #include<stdlib.h> #include "convert.c" int main(){ //declaring character array char **mySentences; int noOfSentences;...

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

  • This code in C converts infix to postfix and evaluates it. The problem is that it...

    This code in C converts infix to postfix and evaluates it. The problem is that it only evaluates one digit expressions. I need to fix it so that it can evaluate 2 digits expressions as well. #include <stdio.h> #include <ctype.h> #include <string.h> #include <math.h> #define SIZE 100 char s[SIZE]; int top=-1; void infixToPostfix(char *infix, char *postfix); void postfixEvaluation(char *postfix); void push(char elem){ s[++top]=elem; } char pop(){ return(s[top--]); } int pr(char elem){ // Order of precedence switch (elem) { case '(':...

  • Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program...

    Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program A3a without modification: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> void getGrades(int ROWS, int COLS, int grades[ROWS][COLS], char students[COLS][20]); void printGrades(int ROWS, int COLS, int grades[ROWS][COLS]); void getStudents(int COLS, char students[COLS][20]); void printStudents(int COLS, char students[COLS][20]); void calcGrades(int ROWS, int COLS, int grades[ROWS][COLS], char Fgrades[]); void printFinalGrades(int COLS, char Fgrades[]); int main() {    srand(time(0));    int stu = 0, assign = 0;...

  • My C program is supposed to make a random key the same length of clear_text string...

    My C program is supposed to make a random key the same length of clear_text string and then use the random key to encrypt clear_text, but I can’t get it to work. A wΝΗ 1 #include <stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 char* make_rand_key(int length, char* key); 5 void encrypt(); 7- int main() { 8 encrypt(); 9 } 10 char* make_rand_key(int length, char* key){ int range=78; int max=48; char c='a'; int i; 'srand(time(NULL)); for(i=0;i<length;i++){ C=rand()%range_max; key[i]=c; key[i]='\0'; return key; 23...

  • Question: For the picture writing question, if the question says that the picture length (height) and...

    Question: For the picture writing question, if the question says that the picture length (height) and width are multiples of 5, then be prepared (for example) to handle a situation where you are being asked to blacken the fourth (vertical) strip from the left. Code: #include #include #include #include #define BUFFER_SIZE 70 #define TRUE 1 #define FALSE 0 int** img; int numRows; int numCols; int maxVal; FILE* fo1; void addtopixels(int** imgtemp, int value); void writeoutpic(char* fileName, int** imgtemp); int** readpic(char*...

  • Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression...

    Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression. Right now, it works with single digits. I need to modify it so that it can evaluate expressions with also 2 digits, example: (60+82)%72. Additionally I need to display an error when the parenthesis don't match like (89+8(. I have muted some line that would print the postfix expression with a space like: 22 8 + when the input...

  • If you already answer this question, please skip, thanks C Programming. Fill in ... This program...

    If you already answer this question, please skip, thanks C Programming. Fill in ... This program will be called with one command line argument that contains a string followed by an asterisk and an integer. Print out the string as many time as indicated by the integer. For example, when called as prog Hi*3, you print HiHiHi. Hint: Look for the '*' starting from the back of the string. len = strlen(arg) gives you the string length. When you have...

  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...

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