Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6674919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:47:20+00:00 2026-05-26T03:47:20+00:00

Exercise 1-22 of The C Programming Language is as follow: Write a program to

  • 0

Exercise 1-22 of The C Programming Language is as follow:

Write a program to “fold” long input lines into two or more shorter
lines after the last non-blank character that occurs before the n-th
column of input. Make sure your program does something intelligent
with very long lines, and if there are no blanks or tabs before the
specified column.

This is the code:

#include <ctype.h>
#include <stdio.h>

#define MAXLINE 500
#define FOLD_LENGTH 15

/* _getline:  read a line into s, return length  */ 
size_t _getline(char s[], int lim) 
{ 
    int c; 
    size_t i;

    for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) 
        s[i] = c; 
    if (c == '\n') { 
        s[i] = c; 
        ++i; 
    } 
    s[i] = '\0'; 
    return i; 
}

int main()
{
    int c;
    char line[MAXLINE];
    char temp;
    unsigned last_space_idx = 0, i, offset = 0;

    while (_getline(line, MAXLINE) != 0) {
        for (i = 0; line[offset+i] != '\0'; i++) {
            if (i == FOLD_LENGTH) {
                temp = line[offset+last_space_idx];
                line[offset+last_space_idx] = '\0';
                printf("%s\n", line+offset);
                line[offset+last_space_idx] = temp;
                offset = last_space_idx;
                i = 0;
                continue;
            }
            if (isspace(line[offset+i])) {
                last_space_idx = offset+i;
            }
        }
        printf("%s\n", line+offset);
    }
    return 0;
}

This is the sample input I’m using:

Penny Lane is in my ears and in my eyes
There beneath
the blue suburban skies

And this is the output I get:

Penny Lane is
 in my ears and in my ey
 and in my eyes

 eyes

 eyes

 eyes

What’s the bug here? I really have no clue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:47:20+00:00Added an answer on May 26, 2026 at 3:47 am

    Lots of errors. You do this:

    last_space_idx = offset+i;
    

    But you also do this:

    temp = line[offset+last_space_idx];
    

    Which means that temp = line[(2 * offset) + last_observed_space_relative_to_offset].

    You also do this:

    offset = last_space_idx;
    

    That means the offset becomes equal to the last observed space, so you’ll have a preceding space on every line after the first, like this:

    Penny lane is
     in my ears
     and in my eyes
    

    Your _getline() method does this:

    if (c == '\n') { 
        s[i] = c; 
        ++i; 
    }
    

    That means any line returns are preserved, so if you have There beneath\nthe blue suburban skies as the input you’ll get this output:

    There beneath
    
    the blue suburban skies
    

    Lastly, each new line you read uses the last space index and offset from the previous line. You need to reset them before the for loop starts.

    Here’s a fixed version. I’ve tidied up the style a little and replaced the printf() bodge with a string format that will print a substring.

    #include <stdio.h>
    
    #include <ctype.h>
    #include <stdio.h>
    
    #define MAXLINE 500
    #define FOLD_LENGTH 15
    
    size_t _getline(char s[], int lim);
    
    /* _getline:  read a line into s, return length  */ 
    size_t _getline(char s[], int lim) {
    
        char c; 
        size_t i;
    
        for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
            s[i] = c;
        }
    
        s[i] = '\0'; 
        return i; 
    }
    
    int main() {
    
        char line[MAXLINE];
        unsigned last_space_idx = 0;
        unsigned i;
        unsigned offset = 0;
    
        while (_getline(line, MAXLINE) != 0) {
    
            last_space_idx = 0;
            offset = 0;
    
            for (i = 0; line[offset+i] != '\0'; ++i) {
                if (i == FOLD_LENGTH) {
                    printf("%.*s\n", last_space_idx, line + offset);
                    offset += last_space_idx + 1;
                    i = 0;
                } else if (isspace(line[offset + i])) {
                    last_space_idx = i;
                }
            }
    
            printf("%s\n", line + offset);
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Problem: Exercise 2-8 of The C Programming Language, Write a function rightrot(x,n) that
I am currently writing a programming language in C/C++ as an exercise (but mostly
In section 1.6 of 'The C Programming Language' (K&R) there is an example program
I'm trying to solve exercise 2-1 from The C Programming Language, 2nd edition, which
I am working through some of the exercises in The C++ Programming Language by
As a programming exercise, I've written a Ruby snippet that creates a class, instantiates
In a c programming exercise I am asked to convert an int to char
I vaguely remember reading about a programming exercise where objects are drawn on the
I am reading about programming, and one exercise involved programming Pascal's triangle in R.
I'd like to implement a big int class in C++ as a programming exercise—a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.