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

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 -.
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...
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 =...
in java Write a program that convert a Fahrenheit to Celsius. For conversion, use as many...
in java Write a program that convert a Fahrenheit to Celsius. For conversion, use as many as methods you want
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...
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...
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,...
I need convert this java code to C language. There is no string can be used...
I need convert this java code to C language. There is no string can be used in C. Thank you! import java.util.Scanner; public class Nthword { public static void main( String args[] ) { String line; int word; Scanner stdin = new Scanner(System.in); while ( stdin.hasNextLine() ) { line = stdin.nextLine(); word = stdin.nextInt(); stdin.nextLine(); // get rid of the newline after the int System.out.println( "Read line: \"" + line + "\", extracting word [" + word + "]" );...
write a program in Java that can take a given text file and then compress it...
write a program in Java that can take a given text file and then compress it with Huffman coding due 20 october 2020 Huffman coding can be used to “zip” a text file to save space. You are required to write a program in Java that can take a given text file and then compress it with Huffman coding. Your program should be able to decompress the zipped files as well [4 marks]. In addition, show percentage gain in data...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT