Question

Question: Can someone explain to me what the if( pos > 0) putchar('\n'); does and why...

Question: Can someone explain to me what the if( pos > 0) putchar('\n'); does and why it does that? Why does it check if the position is greater than 0, and if it is, put a newline character?

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 ntelligent with very long lines, and if there are no blanks or tabs before the specified column.

Solution

/* fold, to fold long lines after a specified Column */

#include

#define MAXCOL 50
#define TABVAL 8

char line[MAXCOL];
int expandtab(int pos);
int printline(int pos);
int getblank(int pos);
int newposition(int pos);

int main(void)
{
    int pos,c;
    pos = 0;

    while((c=getchar())!=EOF)
    {
        line[pos] = c;

        if( c == '\t')
            pos = expandtab(pos);
        if( c == '\n')
        {
            printline(pos);
            pos = 0;
        }
        else if( ++pos >= MAXCOL )
        {
            pos = getblank(pos);
            printline(pos);
            pos = newposition(pos);
        }
    }
    return 0;
}

int expandtab(int pos)
{
    line[pos] = ' ';

    for(++pos; (pos < MAXCOL)&&((pos % TABVAL)!= 0); ++pos)
    line[pos] = ' ';

    if( pos >= MAXCOL)
    {
        printline(pos);
        return 0;
    }
    else
        return pos;
}

int printline(int pos)
{
    int i;
    for(i = 0; i < pos; ++i)
        putchar(line[i]);
    if( pos > 0)
        putchar('\n');
}

int getblank(int pos)
{
    if( pos > 0)
        while( line[pos] != ' ')
            --pos;
    if(pos == 0)
        return MAXCOL;
    else
        return pos + 1;
}
int newposition( int pos)
{
    int i,j;

    if(pos <= 0 || pos >= MAXCOL)
        return 0;
    else
    {
        i = 0;
        for(j=pos;j < MAXCOL; ++j,++i)
        line[i] = line[j];  
    }
    return i;
}
    

Homework Answers

Answer #1

Please find the answer to your question:

what the if( pos > 0) putchar('\n'); does and why it does that?

In the function printline it takes input as the position till the characters arr read for 1 column. Now when it prints the character of the one line first it prints the character to the console using the first for loop:

 for(i = 0; i < pos; ++i)
        putchar(line[i]);

then in the next line it checks if that line actually contains a character or not because if it contains the characters then the value of pos must be greater then 0 and that means that this line needs to end. So it's giving input as the

putchar('\n');

which print the new line to the console. "\n" is an escape sequence to print the new line on the console in the C language and many other languages. It indicates compiler to print the new line on the console whenever there is "\n"

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please answer the following C question: There is a documented prototype for a function called get_a_line...
Please answer the following C question: There is a documented prototype for a function called get_a_line in the code below. Write a definition for get_a_line—the only function called from that definition should be fgetc. #include <stdio.h> #include <string.h> #define BUFFER_ARRAY_SIZE 10 int get_a_line(char *s, int size, FILE *stream); // Does what fgets does, using repeated calls to fgetc, but // provides a more useful return value than fgets does. // // REQUIRES // size > 1. // s points to...
This program will output a right triangle based on user specified height triangle_height and symbol triangle_char....
This program will output a right triangle based on user specified height triangle_height and symbol triangle_char. (1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangle_char character. (1 pt) (2) Modify the program to use a loop to output a right triangle of height triangle_height. The first line will have one user-specified character, such as % or *. Each subsequent line will have...
Write a C program that calculates and displays the sum of all fourdigit numbers in the...
Write a C program that calculates and displays the sum of all fourdigit numbers in the line. The only include to use is <stdio.h>. The only keywords:void, int, char, while,if, else, and return. The only library functions:printf(), getchar().The only predefined constant:EOF. Input            output 3153\n           3153 00020005\n 7 999578585    error 456 5443       error 6574y435      error
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i];...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i]; } return (double)sum/n; } Rewrite the function to eliminate the array subscripting (a[i]), using pointer arithmatic instead. Write a program that reads a line of input and checks if it is a palindrome (ignoring spaces) Write a program that takes the name of a file as a command line argument, and prints the contents of the file (similar to the linux 'cat' program). Write...
Can someone please tell me what each line of this Java code does exactly? LINE BY...
Can someone please tell me what each line of this Java code does exactly? LINE BY LINE PLEASE!!! package p2; public class Course {    // class fields private String code; // course code, such as "EE333" private String name; // course name, such as "Engineering Programming w/ Objects" public Note notes[]; // attached note of a course private int noteSize; // size of the note by lines    // construct a Course with a code and a name, i.e....
This is my code and can you please tell me why it's not working? By the...
This is my code and can you please tell me why it's not working? By the way, it will work if I reduce 10,000,000 to 1,000,000. #include <iostream> using namespace std; void radixSort(int*a, int n) { int intBitSize = sizeof(int)<<3; int radix = 256; int mask = radix-1; int maskBitLength = 8;    int *result = new int[n](); int *buckets = new int[radix](); int *startIndex = new int[radix]();    int flag = 0; int key = 0; bool hasNeg =...
Give the output of the following program. Then explain what the foo() method does, in general,...
Give the output of the following program. Then explain what the foo() method does, in general, for a given input. Is foo() a tail-recursive method? Why or why not? class SomeThing {     public static void main(String args[]) {         System.out.println(foo(6));     }     private static int foo(int input) {         if (input <= 0) {             throw new RuntimeException("invalid input");         }         else if (input == 1) {             return 1;         }         else if (input %...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is always zero? Please fix and explain! #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int BinarySearch(const int A[],const int L,const int R,const int key) {              if (R >= 1){           int middle = L + (R-1)/2;                              if (A[middle] == key)        return A[middle];               if (A[middle] > key){...
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
If you cant answer this please dont waste my question. thank you. This cryptographic program run...
If you cant answer this please dont waste my question. thank you. This cryptographic program run and produce text screen output. You are to create a GUI that uses the program. Your program (GUI) must allow a user to input of a message to be coded. It must also have an area to show the plaintext, the ciphertext, and the decrypted text. If required by your choice of cryptographic method, the user should have an area to input a key....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT