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 {...
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 =...
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 =...
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...
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)...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Plato states in the Phaedo that: “either knowledge is nowhere to be gained, or else it...
    asked 3 minutes ago
  • 1. The nucleus is a membrane-bound organelle that contains the cells genetic information. What other intracellular...
    asked 18 minutes ago
  • Do genetics play a significant role in human attraction? Defend a position (yes or no). Use...
    asked 40 minutes ago
  • 9.14 The quality-control manager at a compact flu-orescent light bulb (CFL) factory needs to determine whether...
    asked 45 minutes ago
  • Describe a business you are familiar with. Discuss ONE way this firm adjusts its short-term capacity...
    asked 59 minutes ago
  • Write a persuasive speech on whether online or face to face learning/education is better for students
    asked 1 hour ago
  • (a) As an analyst briefly explain what you will consider in applying nested designs (b) State...
    asked 1 hour ago
  • Metals are good thermal conductors — that is, when there is a temperature difference across their...
    asked 1 hour ago
  • Theoretical Perspectives that attempts to explain and answer fundamental questions why societies form and why they...
    asked 1 hour ago
  • I am working with data analysis and with a survey. I am cleaning up the following...
    asked 1 hour ago
  • For this problem, carry at least four digits after the decimal in your calculations. Answers may...
    asked 1 hour ago
  • The International Air Transport Association surveys business travelers to develop quality ratings for transatlantic gateway airports....
    asked 1 hour ago