Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //write a program to print all input lines that are longer than 80 characters
- //a not very elegant solution to Exercise 1-17, p31
- //this solution uses two concepts that K&C haven't
- //introduced at this stage: multi arrays and globals.
- //not very happy with this but it does the job
- // ~/cAgain14.c
- //thoughts on better ways to do it: applehelpwriter@icloud.com
- //thanks!
- #include <stdio.h>
- #define LIMIT 1000
- #define TEST_LENGTH 80
- int k; //global
- int getLineLength(char s[], int a);
- void linesToCopy(char t[], char u[], char r[][LIMIT]);
- int main(int argc, const char * argv[]){
- //declare and init:
- int d, e;
- e = k = 0;
- char currentLine[LIMIT];
- char linesToPrint[LIMIT];
- char lines[LIMIT][LIMIT]; //1 megabyte
- while( (d = getLineLength(currentLine, LIMIT) ) > 0){
- if (d > TEST_LENGTH){
- e = d;
- linesToCopy(linesToPrint, currentLine, lines);
- }//end if
- }//end while
- printf("\n\nLines over %d characters:\n", TEST_LENGTH);
- if (e > TEST_LENGTH){
- k = 0;
- while (k < LIMIT && lines[k][d] != '\0'){
- if (lines[k][d] != '\n'){
- printf("\n%d: %s", k, lines[k]);
- }
- else {
- printf("\n");
- }
- ++k;
- }
- }//end if
- else
- printf("Sorry, no lines were more than %d characters\n", TEST_LENGTH);
- return 0;
- }//end main
- //this will return the length of the line
- int getLineLength(char s[], int a){
- int c, i;
- for (i = 0; i < LIMIT-1 && (c = getchar()) !=EOF && c != '\n'; ++i)
- s[i] = c;
- if (c == '\n'){
- s[i] = c;
- ++i;
- }//end if
- s[i] = '\0';
- return i;
- }//end getLineLength
- void linesToCopy(char t[], char u[], char r[][LIMIT]){
- int i;
- for (i = 0; (r[k][i] = u[i]) != '\0'; ++i){
- if (u[i] == '\n')
- ++k;
- }//end for
- }//end linesToCopy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement