Question

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 -ea Levenshtein.

code:

public class Levenshtein
{

private static int testsFailed = 0;
private static int testsExecuted = 0;

public static void main( String[] args )
{
System.out.println( "Testing typical cases.\n" );
testDistance( "kitten", "kitten", 0 );
testDistance( "kitten", "sitting", 3 );
testDistance( "kitten", "mittens", 2 );
testDistance( "balloon", "saloon", 2 );
testDistance( "hello", "doggo", 4 );
testDistance( "\t\thi", "\t\t\t\thi", 2 );

System.out.println( "\nTesting empty/edge cases.\n" );
testDistance( "", "", 0 );
testDistance( "hello", "", 5 );
testDistance( "", "doggo", 5 );
testDistance( "a", "b", 1 );
testDistance( "b", "b", 0 );
testDistance( " ", " ", 0 );

System.out.println( "\nThis might take a while...\n" );
testDistance( "12345678901", "123456789012", 1 ); // how many chars are we looking at?

System.out.println( "\n******These tests will be opposite in the C version******\n" );
System.out.println( "\n******These tests **should** FAIL in the C version*******\n" );
testDistance( "kitten", "mitten\0s", 3 ); // ????
testDistance( "\0totally", "\0different", 9 );

System.out.println( "\nTotal number of tests executed: " + testsExecuted );
System.out.println( "Number of tests passed: " + (testsExecuted - testsFailed) );
System.out.println( "Number of tests failed: " + testsFailed );
}

public static void testDistance( String s, String t, int expected )
{
int distance = levenshtein( s, t );

if ( distance == expected )
{
System.out.println( "Passed! You can get from '" + s + "' to '" + t + "' in " + expected + " moves." );
}
else
{
System.out.println( "FAILED: I thought it would take " + expected + " moves, but apparently it takes " + distance +
" moves to get from '" + s + "' to '" + t + "'." );
testsFailed++;
}

testsExecuted++;
}


public static int levenshtein( String s, String t )
{
int cost;
int distance;
String deleteS;
String deleteT;

if (s.length() == 0)
{
distance = t.length();
}
else if (t.length() == 0)
{
distance = s.length();
}
else
{
if (s.charAt(0) == t.charAt(0))
{
cost = 0;
}
else
{
cost = 1;
}

deleteS = s.substring(1);
deleteT = t.substring(1);

assert(deleteS.length() == s.length() - 1);
assert(deleteT.length() == t.length() - 1);

assert(s.endsWith(deleteS));
assert(t.endsWith(deleteT));

distance = minimum(new int[] { levenshtein(deleteS, t) + 1,
levenshtein(s, deleteT) + 1,
levenshtein(deleteS, deleteT) + cost });
}

return distance;
}

public static int minimum( int minimum[] )
{
int min = 0;
assert( minimum.length > 0 );

if ( minimum.length > 0 )
{
min = minimum[0];

for ( int i = 1; i < minimum.length; i++ )
{
if ( minimum[i] < min )
{
min = minimum[i];
}
}
}

return min;
}
}

Homework Answers

Answer #1

solution:

Working code implemented in C and appropriate comments provided for better understanding:

Source code for levenshtein.c:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#define DIST_SIZE 3 //Defines the size of the array dist[] initalized in levenshtein()
static int testsExecuted = 0;
static int testsFailed = 0;

static int minimum(int m[],int sizeDist)
//recieves a array of ints to compensate for java's varargs
//returns the minimum int of all the values in m[]
{
int min = 0;
assert(sizeDist > 0);
if(sizeDist > 0)
{
min = m[0];
for(int i = 1; i <sizeDist; i++)
{
if(m[i] < min)
{
min = m[i];
}
}
}
return min;
}

static void substring(char a[],char b[],int pos)
//recieves two strings from levenshtein()
//makes b[] into a substring of a[], from position 1 in a[] to the end of a[]
//equivalent to Java's String method substring(int)
//only works when pos is 1, specific to the java version substring input
//does not return anything
{
while(a[pos] != '\0') //start at the position given from levenstein()
{
b[pos-1] = a[pos]; //since we want b[] to start at 0, pos - 1
pos++;
}
b[pos-1] = '\0'; //have to add in the null terminator at the end of the substring b[]
}


static int lengthString(char a[])
//gets the length of a given string
//does ot include the null terminator
//was using strlen at first, but since strlen returns a long it...
//...didnt work very well with the majority of other types (int)
{
int count = 0;
while(a[count] != '\0')
{
count++;
}
return count;
}

#define T 1; //Define True = 1
#define F 0; //Define False= 0
static int endsWith(char a[], char b[])
//checks if the arrays are equal
//equivalent to java's endsWith() method
{
int i = 0;
int equals = T;
while((a[i+1] != '\0') && (b[i] != '\0'))
{
if(a[i+1] != b[i]) //a[i+1] is used because b[] is (at this point) a...
{ //...substring of a[], which holds on less value than a[]...
equals = F; //...compensating for int i being at the start of b[]
}
i++;
}
return equals;
}
static int levenshtein(char s[],char t[])
//recieves strings s[] and t[] from testDistance() to compute moves
{
int cost;
int distance;
char deleteS[strlen(s)];
char deleteT[strlen(t)];
  
if(strlen(s) == 0)
{
distance = (int)strlen(t);
}
else if(strlen(t) == 0)
{
distance = (int)strlen(s);
}
else
{
if(s[0] == t[0])
{
cost = 0;
}
else
{
cost = 1;
}
substring(s,deleteS,1); //substring is called here with pos = 1
substring(t,deleteT,1); //substring is called here with pos = 1
  
int ds = lengthString(deleteS); //ds stands for deleteSLength
int dt = lengthString(deleteT); //dt stands for deleteTLength
int sLenght = lengthString(s);
int tLenght = lengthString(t);
  
assert(ds == (sLenght-1)); //checking deleteSLength is s[] length minus 1
assert(dt == (tLenght-1)); //checking deleteTLength is t[] length minus 1
  
assert(endsWith(s,deleteS)); //endsWith check function called here
assert(endsWith(t,deleteT)); //endsWith check function called here
  
int d0 = levenshtein(deleteS, t) + 1; //I put each recursion call into its own...
int d1 = levenshtein(s, deleteT) + 1; //...variable for more organization and...
int d2 = levenshtein(deleteS, deleteT) + cost; //... easier debugging
int dist[] = {d0,d1,d2};
distance = minimum(dist,DIST_SIZE);
}
return distance;
}

static void testDistance(char s[],char t[], int expected)
{
int distance = levenshtein(s, t);
if(distance == expected)
{
printf("Passed! You can get from '%s' to '%s' in %d moves.",s,t,expected);
printf("\n");
}
else
{
printf("Failed: i thought it would take %d moves, but apparently it takes %d moves to get from '%s' to '%s'.",expected,distance,s,t);
printf("\n");
testsFailed++;
}
testsExecuted++;
}
int main()
{
printf("Testing typical cases.\n" );
testDistance( "kitten", "kitten", 0 );
testDistance( "kitten", "sitting", 3 );
testDistance( "kitten", "mittens", 2 );
testDistance( "balloon", "saloon", 2 );
testDistance( "hello", "doggo", 4 );
testDistance( "\t\thi", "\t\t\t\thi", 2 );
  
  
printf( "\nTesting empty/edge cases.\n" );
testDistance( "", "", 0 );
testDistance( "hello", "", 5 );
testDistance( "", "doggo", 5 );
testDistance( "a", "b", 1 );
testDistance( "b", "b", 0 );
testDistance( " ", " ", 0 );
printf( "\nThis might take a while...\n" );
testDistance( "12345678901", "123456789012", 1 ); // how many chars are we looking at?
  
printf( "\nThese tests will not succeed in the C version\n" );
testDistance( "kitten", "mitten\0s", 3 ); // ????
testDistance( "\0totally", "\0different", 9 );
  
printf( "\nTotal number of tests executed: %d",testsExecuted );
printf("\n");
printf( "Number of tests passed: %d", testsExecuted - testsFailed);
printf("\n");
printf("Number of tests failed: %d",testsFailed);
printf("\n");
printf("Program completed normally.\n");
return 0;
}

Output Screenshots:

Hope it helps, if you like the answer give it a thumbs up. 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. 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...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
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 {...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1 = new String("hello"); String s2 = "hello"; String s3 = "hello";    System.out.println(s1 == s3); System.out.println(s1.equals(s3)); System.out.println(s2 == s3); } } When we run the program, the output is: false true true Explain why this is the output, using words and/or pictures.
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 =...
Design a program that calculates the amount of money a person would earn over a period...
Design a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT