Question

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 w
          // ord
          System.out.print(" ");
          position++;
        }
      }   
    }      
  }
}

Please note the output is supposed to change the input in.txt from this;

Introduction

I've
been
involved
with
XP
for
a
couple
of
years
now
and
where
I've
seen
XP
implemented
properly
it
seems
to
have
worked
extremely
well.
But
there

to this;

Introduction

I've been involved with XP for a couple of years
now and where I've seen XP implemented properly it
seems to have worked extremely well. But there
does seem to be an awful lot of opponents to XP in
the industry and this baffles my colleagues and I
at eXoftware. To us XP is just a better way of
producing code and we just can't imagine any
coding shop not wanting to produce better code. We
think that the reason some people and
organisations are antagonistic to XP (and the
arguments do get very heated sometimes) is
culture. Their cultures simply don't allow
practices such as those proposed by XP.

Homework Answers

Answer #1

SOLUTION-
I have solve the problem in C code with comments and screenshot for easy understanding :)

CODE-

//c code for java
#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-


IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------

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
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. 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 {...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or start with the link below: https://www.thoughtco.com/using-the-arraylist-2034204 import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + ".");...
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program....
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program. Tip: You have explicit permission to copy/paste the main method for this question, replacing instances of System.out.println with printf, where appropriate. You can get the assert function from assert.h. Try running the Java program on the CS macOS machines. You can compile and run the program following the instructions discussed in class. Assertions are disabled by default. You can enable assertions by running java...
using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the...
using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the countdown sequence for a rocket launch. However, it seems to loop infinitely. Fix the program so it counts down and terminates properly. starter code : import java.util.Scanner; /* * Counts down to a blastoff, starting from a given number. */ public class Countdown {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int countdown = 0;...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...