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 =...
Question:   Can you please convert this program into switch case program using c++. Output should be...
Question:   Can you please convert this program into switch case program using c++. Output should be same int main() {     bool weekend = false;     unsigned char day = 0;     bool isDay = true;     cout << " Enter a char for a day of week:";     cin >> day ;     if (day == 'M' || day == 'm')         cout << " This is Monday" << endl ;     else if (day == 'T' || day...
4 Implement a Java program that meets the following requirements • You can use the Java...
4 Implement a Java program that meets the following requirements • You can use the Java standard sequence data structure API types for sets, lists, stack,queue and priority queue as needed. All are available in the java.util package, which you will want to import in your program. 1. Argue in code comments which data structure, stack or queue, you will use to implement this method. Implement a method which creates some String objects as food orders for a small restaurant,...
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.
Convert the attached C++ code to working Java code. Be judicious in the change that you...
Convert the attached C++ code to working Java code. Be judicious in the change that you make. This assignment is not about re-writing or improving this code, but rather about recognizing the differences between C++ and Java, and making the necessary coding changes to accommodate how Java does things. PLEASE DO NOT use a built-in Java QUEUE (or any other) container. Additional resources for assignment: #include <iostream> #include <string> using namespace std; class pizza { public: string ingrediants, address; pizza...
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...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT