Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Exercise 1-16. Revise the main routine of the longest-line program so it will correctly print the length of arbitrary long input lines, and as much as possible of the text.*/
- #include <stdio.h>
- #define MAX 1000
- int get_line(char str[], int length);
- void copyStr(char str[], char dest[], int length);
- int main()
- {
- int length = MAX, max = 0;
- char str[MAX], dest[MAX];
- while ((length = get_line (str, MAX)) > 0)
- if (length > max) {
- max = length;
- copyStr(str, dest, MAX);
- }
- printf("%d %s\n", max, dest);
- return 0;
- }
- int get_line(char str[], int length) {
- int c, i = 0;
- while ((c = getchar()) != EOF && c != '\n' && i < length - 1)
- str[i++] = c;
- str[i] = '\0';
- return i;
- }
- void copyStr(char str[], char dest[], int length) {
- int i = 0;
- while ((dest[i] = str[i]) != '\0' && i < length - 1)
- i++;
- dest[i] = '\0';
- }
Advertisement
Add Comment
Please, Sign In to add comment