Path: blob/master/languages/cprogs/Ex_1.16_LongLine.c
1240 views
/**1* Exercise 1.162*3* Write a program to print the length of an arbitrarily long input line4* and as much text as possible5*6**/789#include <stdio.h>1011/* The size of the output that we would like */12#define MAX 10001314/* define our functions*/15int getNewLine(char line[], int max);16void copy(char to[], char from[]);1718int main(){1920int len, max;21char line[MAX];22char longest[MAX];2324max = 0;25while((len = getNewLine(line, MAX)) > 0){2627if(len > max){2829max = len;30copy(longest, line);3132}3334}3536if (max > 0){3738printf("length = %i, string=%s", max, longest);3940}4142return 0;4344}4546/* get a line in a character array */47int getNewLine(char arr[], int lim){4849int c, i;5051for(i=0; i < lim -1 && (c=getchar()) != EOF && c != '\n'; ++i){5253arr[i] = c;5455}56if(c == '\n'){5758arr[i] = c;59++i;6061}6263else{6465/* Continue to count the length even if it is longer than the max */66while((c=getchar()!=EOF) && c != '\n'){6768++i;6970}7172if(c == '\n'){7374arr[i] = c;75++i;7677}7879}8081arr[i] = '\0';82return i;8384}8586/* copy one character array to another */87void copy(char to[], char from[]){8889int i;9091i = 0;9293while((to[i] = from[i]) != '\0'){9495++i;9697}9899}100101102