mihaild

Untitled

Aug 26th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #define MAXLINE 1000 /* максимальный размер вводимой строки */
  3.  
  4. int my_getline(char s[], int lim);
  5. void copy(char to[], char from[]);
  6.  
  7. /* печать самой длинной строки */
  8. main()
  9. {
  10.     int len; /* длина текущей строки */
  11.     int max; /* длина максимальной из просмотренных строк */
  12.     char line[MAXLINE]; /* текущая строка */
  13.     char longest[MAXLINE]; /* самая длинная строка */
  14.  
  15.     max = 0;
  16.     while ((len = my_getline(line, MAXLINE)) > 0)
  17.         if (len > max) {
  18.             max = len;
  19.             copy(longest, line);
  20.         }
  21.     if (max > 0) /* была ли хоть одна строка? */
  22.         printf("%s", longest);
  23.     return 0;
  24. }
  25.  
  26. /* getline: читает строку в s, возвращает длину */
  27. int my_getline(char s[], int lim)
  28. {
  29.     int c, i;
  30.  
  31.     for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i) {
  32.         s[i] = c;
  33.     }
  34.     if (c == '\n') {
  35.         s[i] = c;
  36.         ++i;
  37.     }
  38.     s[i] = '\0';
  39.     return i;
  40. }
  41.  
  42. /* copy: копирует из 'from' в 'to'; to достаточно большой */
  43. void copy(char to[], char from[])
  44. {
  45.     int i;
  46.  
  47.     i = 0;
  48.     while ((to[i] = from[i]) != '\0')
  49.         ++i;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment