Thread: Noob help

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    Noob help

    Hey hi all! i am reading The C Programming Lenguage, doing the exercies and i need some help

    Exercise 1-17. Write a program to print all input lines that are longer than 80 characters.

    Code:
    #include <stdio.h>
    
    #define MIN 2              /* print lines longer than this one */
    #define MAXLENGHT  10000     /* the main arrays where all the lines will we saved */
    #define MAXLINE  1000      /* mac line length */
    
    int getline(char s[], int lim);
    void copy_to_lines(char from[], char to[], int position, int len);
    int strlen(char s[]);
    
    int main()
    {
        char line[MAXLINE];
        char lines_saved[MAXLENGHT];
        int current_position = 0;
        int len;
    
        while(( len = getline(line, MAXLINE)) > 0)
        {
            if (len > MIN) {
                copy_to_lines(line, lines_saved, current_position, len);
                current_position += len;
            } 
        }
        printf("%s", lines_saved);
    
        return 0;
    }
    
    int getline(char s[], int lim)
    {
        int c, i;
    
        for (i = 0; i < lim && (c = getchar()) != EOF && c != '\n'; ++i)
        {
            s[i] = c;
        }
        if (c == '\n') {
            s[i] = c;
            ++i;
        } 
    
        return i;
    }
    
    void copy_to_lines(char from[], char to[], int position, int len)
    {
        int i;
    
        for(i = 0; i < len; ++i)
        {
            to[position] = from[i];
            ++i;
            ++position;
        }
    }
    I am not using any resource not present in the book at that point.
    Any help is much appreciated.
    Thanks.
    Last edited by Alejandrito; 02-09-2009 at 08:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob Q: How to read a value of a byte in binary?
    By Hitsuin in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 02:46 PM
  2. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  3. I'm a noob and I need help.
    By nifear4 in forum C Programming
    Replies: 17
    Last Post: 10-14-2008, 01:20 PM
  4. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 08:49 AM
  5. noob: compiling error... plz help!
    By chosii in forum C Programming
    Replies: 2
    Last Post: 05-10-2002, 05:53 AM

Tags for this Thread