samir82show

c_ch01_ex16.c

Oct 8th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. /*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.*/
  2. #include <stdio.h>
  3. #define MAX 1000
  4. int get_line(char str[], int length);
  5. void copyStr(char str[], char dest[], int length);
  6. int main()
  7. {
  8. int length = MAX, max = 0;
  9. char str[MAX], dest[MAX];
  10. while ((length = get_line (str, MAX)) > 0)
  11. if (length > max) {
  12. max = length;
  13. copyStr(str, dest, MAX);
  14. }
  15. printf("%d %s\n", max, dest);
  16. return 0;
  17. }
  18. int get_line(char str[], int length) {
  19. int c, i = 0;
  20. while ((c = getchar()) != EOF && c != '\n' && i < length - 1)
  21. str[i++] = c;
  22. str[i] = '\0';
  23. return i;
  24. }
  25. void copyStr(char str[], char dest[], int length) {
  26. int i = 0;
  27. while ((dest[i] = str[i]) != '\0' && i < length - 1)
  28. i++;
  29. dest[i] = '\0';
  30. }
Advertisement
Add Comment
Please, Sign In to add comment