Exercise
C to java converter
Objetive
Create a program to convert simple C programs, such as the following one, to java:
Note: the resulting program must compile correctly. Test it with other similar C programs.
Example Code
import java.util.*;
public class Main
{
public static void main(String[] args)
{
System.out.print("Name of file: ");
String name = new Scanner(System.in).nextLine();
if (!(new java.io.File(name)).isFile())
{
System.out.println("Not found!");
}
else
{
try
{
java.io.InputStreamReader fileC = new java.io.InputStreamReader(name);
java.io.OutputStreamWriter fileCSharp = new java.io.OutputStreamWriter(name + ".cp");
String line;
do
{
line = fileC.ReadLine();
if (line != null)
{
line = line.replace("#include ", "using System;/npublic class Example{");
line = line.replace("int main()", "public static void Main()");
if (line.contains("scanf("))
{
line = line.replace("scanf(\"%d\", &", "");
line = line.replace(");", " = Convert.ToInt32(Console.ReadLine());");
}
line = line.replace("printf", "Console.Write");
line = line.replace("%d", "{0}");
}
} while (line != null);
}
catch (RuntimeException e)
{
System.out.println("Error, " + e.getMessage());
}
}
}
}