Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* analyses the file and prints the longest string */
- /* pointer arithmetics:
- *
- * push(pointer, v) INDICATES *(pointer++) = v
- * pop(pointer) INDICATES *(--pointer)
- * (*++v)[0] INDICATES (**++v)
- * INDICATES first char in v
- * INDICATES name of string/vector v
- * likewise, *v[0] INDICATES **v
- * and *v[n] INDICATES **(v + n)
- * returntype (*funct)(args) INDICATES a function funct with arguments args which returns...
- * char **argv INDICATES pointer to char pointer
- * int(*v)[len] INDICATES pointer "v" to a vector of "len" int elements
- * int *v[len] INDICATES vector "v" of "len" pointers to int elements
- * void *funct() INDICATES function "funct" that returns a pointer-to-void
- * void (*funct)() INDICATES pointer to a function "funct" that returns void
- *
- */
- /* useful characters: [] # */
- # include <stdio.h>
- # define MAXL 1000 // maximum line length
- /* copies the string to another char array */
- void copy(char to[], char from[])
- {
- int i = 0;
- for(i = 0; (to[i] = from[i]) != '\0'; i++)
- // stops copying when the source is out of chars
- ;
- }
- /* returns the length of a line */
- int getlineX(char line[], int limit)
- {
- int c, i;
- for(i = 0; i < limit-1 && (c = getchar()) != EOF && c != '\n'; i++)
- {
- line[i] = c; // the new char is copied
- if(c == '\n') // if end of line, the new char is copied
- {
- line[i] = c;
- ++i;
- }
- }
- line[i] = '\0'; // marks end of line
- return i;
- }
- /* main */
- int main(int argc, char * argv[])
- {
- int length, max; // length of the current, and maximum, string
- length = max = 0;
- char lines[MAXL]; // input line
- char longest[MAXL]; // longest line
- while((length = getlineX(lines, MAXL)) > 0) // examines all line lengths
- {
- if(length > max)
- {
- max = length;
- copy(longest, lines); // updates longest line
- }
- }
- if(max > 0)
- printf("%s\n", longest); // prints the longest line, if non-null
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement