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...
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...
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 %...
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 =...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik //...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik // // This class specifies the functions to solve the n-queens // puzzle. //*************************************************************** class nQueensPuzzle { public: nQueensPuzzle(int queens = 8);     //constructor     //Postcondition: noOfSolutions = 0; noOfQueens = queens;     // queensInRow is a pointer to the array     // that store the n-tuple.     // If no value is specified for the parameter queens,     // the default value, which is 8, is assigned to it. bool...
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input....
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input. The first integer indicates the number of values that will follow. Read that many values, and return their sum, ignoring any additional values that may follow. However, if there are fewer integers than specified in the input, print "Error" and terminate. Hint: int n, success; ... success = scanf("%d", &n); // FIRST INTEGER INPUT reads an integer from stdin, returning 1 if the integer...
Below is the problem that I have and here is the code that I have in...
Below is the problem that I have and here is the code that I have in c++. Can someone help me on what I am doing wrong or the correct code. A teacher has asked all her students to line up according to their first name. For example, in one class Amy will be at the front of the line, and Yolanda will be at the end. Write a program that prompts the user to enter the number of students...
CS4315 Operating Systems Lab 2: Linux Processes This lab assignment contains three questions. To submit this...
CS4315 Operating Systems Lab 2: Linux Processes This lab assignment contains three questions. To submit this lab assignment, please use a Word document to include the screenshots and write your answer. 1. Run the following C program, and submit a screenshot of the result. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int main( ) { pid_t pid; if ( (pid = fork()) == 0 ) { printf (“I am the child, my pid = %d and my parent pid = %d\n”,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT