Question
Convert LongWord.java to the equivalent C program named longword.c. While you are converting it into C, make following changes: file read and write: read lines
Convert LongWord.java to the equivalent C program named longword.c. While you are converting it into C, make following changes:
file read and write: read lines from standard input and print the results to standard output
string return in getLongestWord: take a pointer to a pre-allocated buffer and save the longest word in it.
Hint: your function will look like void getLongestWord( const char line[], char word[] )
That is, your converted C program should continuously read lines from standard input, find the longest word using the function getLongestWord, and print the results to standard output. The program ends when it hits EOF. Make sure that your converted C program generates the same output to the Java programs.
Longword.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class LongWord
{
public static void main( String[] args )
{
BufferedReader fileIn = null;
PrintWriter fileOut = null;
String inputLine;
String longWord;
int lineCount = 0;
try
{
fileIn = new BufferedReader( new FileReader( "cat_wiki.txt" ) );
fileOut = new PrintWriter( new FileWriter( "out.txt" ) );
inputLine = fileIn.readLine( );
while( inputLine != null )
{
lineCount++;
longWord = getLongestWord( inputLine );
if( longWord.length( ) > 0 )
{
fileOut.println( "Line #" + lineCount + ": " + longWord );
}
inputLine = fileIn.readLine( );
}
fileIn.close( );
fileOut.close( );
}
catch( IOException ex )
{
System.out.println( ex.getMessage( ) );
ex.printStackTrace( );
}
finally
{
if( fileIn != null )
{
try
{
fileIn.close( );
}
catch( IOException ex )
{ // Ignore. Errors should be already handled above
}
}
if( fileOut != null )
{
fileOut.close( );
}
}
}
public static String getLongestWord( String line )
{
String longest = "";
int longestLength = 0;
String current = "";
int currentLength = 0;
int pos = 0;
char ch;
while( pos < line.length( ) )
{
ch = line.charAt( pos );
if( ch == ' ' || ch == '\t' || ch == ' ' || ch == ' ' )
{
if( currentLength > longestLength )
{
longest = current;
longestLength = currentLength;
}
current = "";
currentLength = 0;
}
else if( (ch == '\'' || ch == '-') && pos > 0 && Character.isLetter( line.charAt( pos - 1 ) ) && pos < line.length( ) - 1 && Character.isLetter( line.charAt( pos + 1 ) ) )
{
current += ch;
}
else
{
current += ch;
currentLength++;
}
pos++;
}
if( currentLength > longestLength )
{
longest = current;
longestLength = currentLength;
}
return longest;
}
}
Cat_wiki.txt
Retrieved from https://en.wikipedia.org/wiki/Cat on January 5, 2018 INTRODUCTION The domestic cat[1][5] (Felis silvestris catus or Felis catus) is a small, typically furry, carnivorous mammal. They are often called house cats[6] when kept as indoor pets or simply cats when there is no need to distinguish them from other felids and felines. They are often valued by humans for companionship and for their ability to hunt vermin. There are more than seventy cat breeds recognized by various cat registries. Cats are similar in anatomy to the other felids, with a strong flexible body, quick reflexes, sharp retractable claws, and teeth adapted to killing small prey. Cat senses fit a crepuscular and predatory ecological niche. Cats can hear sounds too faint or too high in frequency for human ears, such as those made by mice and other small animals. They can see in near darkness. Like most other mammals, cats have poorer color vision and a better sense of smell than humans. Cats, despite being solitary hunters, are a social species and cat communication includes the use of a variety of vocalizations (mewing, purring, trilling, hissing, growling, and grunting), as well as cat pheromones and types of cat-specific body language.[7] Cats have a high breeding rate.[8] Under controlled breeding, they can be bred and shown as registered pedigree pets, a hobby known as cat fancy. Failure to control the breeding of pet cats by neutering, as well as the abandonment of former household pets, has resulted in large numbers of feral cats worldwide, requiring population control.[9] In certain areas outside cats' native range, this has contributed, along with habitat destruction and other factors, to the extinction of many bird species. Cats have been known to extirpate a bird species within specific regions and may have contributed to the extinction of isolated island populations.[10] Cats are thought to be primarily responsible for the extinction of 33 species of birds, and the presence of feral and free-ranging cats makes some otherwise suitable locations unsuitable for attempted species reintroduction.[11] Since cats were venerated in ancient Egypt, they were commonly believed to have been domesticated there,[12] but there may have been instances of domestication as early as the Neolithic from around 9,500 years ago (7,500 BC).[13] A genetic study in 2007[14] concluded that all domestic cats are descended from Near Eastern wildcats, having diverged around 8,000 BC in the Middle East.[12][15] A 2016 study found that leopard cats were undergoing domestication independently in China around 5,500 BC, though this line of partially domesticated cats leaves no trace in the domesticated populations of today.[16][17] A 2017 study confirmed that domestic cats are descendants of those first domesticated by farmers in the Near East around 9,000 years ago.[18][19] As of a 2007 study, cats are the second most popular pet in the US by number of pets owned, behind freshwater fish.[20] In a 2010 study they were ranked the third most popular pet in the UK, after fish and dogs, with around 8 million being owned.[21]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started