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 { 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 that the program should take 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
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
and change it to this output;
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.
Organisational culture
Organisational culture can be defined as "the
predominating attitudes and behaviour that
characterise the functioning of the organisation".
Organisational culture tells how and what to do to
succeed within the company but where does culture
come from?
Determination of culture
The program should change the input to this output
#include <stdio.h>
#include <string.h>
int main(void) {
int LINE_SIZE = 50;
int position = 1;
char * line = NULL;
size_t len = 0;
char output[60];
int i=0;
int j=0;
FILE *fptr = NULL;
fptr = fopen(fname, "r");
if(fptr == NULL) { printf("Error"); exit(1); }
while ((getline(&line, &len, fptr)) != -1) {
for(i=0; line[i] != '\n'; i++)
output[j++] = line[i];
if (line == '\0')
{
if (position > 1)
{
printf("\n");
}
printf("\n");
position = 1;
}
else
{
if ((position+strlen(line)-1) > LINE_SIZE)
{
output[j] = '\0';
printf("%s", output);
printf("\n");
position = 1;
j = 0;
}
position += strlen(line);
if (position <= LINE_SIZE)
{ // add a blank after the current word
output[j++] = ' ';
position++;
}
}
}
fclose(fptr);
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.