Question

In: Computer Science

Take the Java program Pretty.java and convert it to the equivalent C program. You can use...

Take the Java program Pretty.java and convert it to the equivalent C program. You can use the file in.txt as sample input for your program.

v  import java.io.*;
import java.util.*;

public class Pretty
{
  public static final int LINE_SIZE = 50;
  
  public static void main(String[] parms)
  {
    String inputLine;
    int position = 1;
    Scanner fileIn = new Scanner(System.in);

    while (fileIn.hasNextLine())
    {
      inputLine = fileIn.nextLine();

      if (inputLine.equals(""))
      {
        if (position > 1)
        {
          System.out.println();
        }
          
        System.out.println();
        position = 1;
      }
        
      else
      {
        if ((position+inputLine.length()-1) > LINE_SIZE)
        {
          System.out.println();
          position = 1;
        }
        System.out.print(inputLine);
          
        position += inputLine.length();
        if (position <= LINE_SIZE)
        {  // add a blank after the current word
          System.out.print(" ");
          position++;
        }
      }   
    }      
  }
}

Solutions

Expert Solution

Take input file as in.txt :

sknkbakbhbhkabbkbsbak nsjbnkjb  kabkhb bkebkb 

jwbkb wsb   hkws   bswb bkbse nkbs bekb bejkbk ebkhb jksebkjb bsekb hesbhb hbeshkb hbeskhb hksebvhkbsk esbhkb hbheb
jksbwb bkbk  kjbkjbk
sjkbkeb nejkbnkb jbekjb 
enkbk  kjebske  jbkwhebsk
kbshjb 
ebhbk 

C program :

#include <stdio.h>
#include <string.h>
#define LINE_SIZE 50

int main(int argc, char* argv[])
{
    FILE* file = fopen("in.txt", "r");   // reading the input file in.txt
    char line[1000000];                  // char array to store content in lines
    
    int position = 1;                    // set position to 1
    while (fgets(line, sizeof(line), file))  // runs while loop until all the content of file is read
    {
        if(strcmp(line,"")==0)           // if the line in a file is empty
        {
            if(position > 1)             // if position is greater than 1 print a new line
            {
                printf("\n");            
            }
            printf("\n");
            position =1;                 // set the position as 1
        }
        else                             // if line in a file is not empty
        {
            if((position + sizeof(line)-1) > LINE_SIZE)   // if condition is true
            {
                printf("\n");                            // print a new line 
                position = 1;                            // set position =1
            }
            printf("%s",line);                          // print the current line

            position += sizeof(line);                   // sizeof is used to calculate length of the line 
            if(position <= LINE_SIZE)
            {
                printf(" ");
                position++;
            }
        }
        
    }


    fclose(file);    // close the file 

    return 0;
}

Screenshot and Output :


Related Solutions

Write a program that implements the follow disk scheduling algorithms. You can use C or Java...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java for this assignment. FCFS SSTF SCAN C-SCAN LOOK C-LOOK Your program will service a disk with 5000 cylinders (numbered 0 to 4999). Your program will generate a random initial disk head position, as well as a random series of 1000 cylinder requests, and service them using each of the 6 algorithms listed above. Your program will report the total amount of head movement required...
In C program. Read and convert a sequence of digits to its equivalent integer. Any leading...
In C program. Read and convert a sequence of digits to its equivalent integer. Any leading white space should be skipped. The conversion should include digit characters until a non-digit character is encountered. Modify the program so it can read and convert a sequence of digit characters preceded by a sign, + or -.
Convert this C++ program exactly as you see it into x86 assembly language: // Use the...
Convert this C++ program exactly as you see it into x86 assembly language: // Use the Irvine library for the print function #include <iostream> // The string that needs to be printed char word[] = "Golf\0"; // Pointer to a specific character in the string char * character = word; //NOTE: This main() function is not portable outside of Visual Studio void main() { // Set up a LOOP - See the while loop's conditional expression below int ecx =...
Directions: Convert the following problems below to c++ equivalent code Problem 3: Take any 2 digit...
Directions: Convert the following problems below to c++ equivalent code Problem 3: Take any 2 digit number from 31 to 99. (You do not have to check the numbers just assume the user will enter a valid number so long as you tell them to). Store an extra copy of this number for later 3.   Add 82 to the number. 4.    Remove the hundreds place from your number 5.    Add 1 to the number. 6.    Subtract this from your...
Write a java program that will ask for a Star Date and then convert it into...
Write a java program that will ask for a Star Date and then convert it into the corresponding Calendar date. without using array and methods.
Done in C language using mobaxterm if you can but use basic C This program is...
Done in C language using mobaxterm if you can but use basic C This program is broken up into three different functions of insert, delete, and main. This program implements a queue of individual characters in a circular list using only a single pointer to the circular list; separate pointers to the front and rear elements of the queue are not used. The linked list must be circular.      The insert and remove operations must both be O(1)    You...
i need a pseudocode for a program in java that will convert dollars into euros and...
i need a pseudocode for a program in java that will convert dollars into euros and japanese yen using the print and prinln methods an if, if -else, statement a switch statement a while statement utilizes the file class uses the random class and random number generator and uses at least three methods other than main
The assignment: C++ program or Java You need to use the following programming constructs/data structures on...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on this assignment. 1. A structure named student which contains:   a. int ID; student id; b. last name; // as either array of char or a string c. double GPA;    2. An input file containing the information of at least 10 students. 3. An array of struct: read the student information from the input file into the array. 4. A stack: you can use the...
Can you write a small program in c++ demonstrate the use of pointer (*) & (&)....
Can you write a small program in c++ demonstrate the use of pointer (*) & (&). Can you also explain the pointer system and how to use them. Thank you.
Write a program to convert an NFA to an equivalent DFA. The NFA may contain ε...
Write a program to convert an NFA to an equivalent DFA. The NFA may contain ε transitions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT