Jump to content

Can someone translate this from C to Java?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
camdaddy09

camdaddy09

    Newbie

  • Members
  • PipPip
  • 15 posts
#include <stdio.h>


int main (int argc, const char * argv[]) 

{

	char

	array[30];

	int

	cnt;

	

	FILE

	*ifp;

	

	ifp = fopen( "alphabet.dat","r");

	if(ifp)

	{

		for(cnt = 0; cnt < 26; cnt += 1)

		{							// prints alphabet

		  fscanf(ifp, "%c",&array[cnt]);

		  printf("%c",array[cnt]);

		  }

		  

		printf("\n");

		

		for(cnt = 0; cnt < 10; cnt += 1)

		{							// prints first 10

			printf("%c",array[cnt]);

		}

		

		printf("\n");

		

		for(cnt = 20; cnt < 26; cnt += 1)

		{							// prints last 6

			

			printf("%c",array[cnt]);

		}

		

		printf("\n");

		

		printf("%c\n",array[10]);			// prints 11th letter

		

		for(cnt = 26; cnt  > -1 ;cnt -= 1)

		{							// prints backwards

			printf("%c",array[cnt]);

		}

		

		printf("\n");

		

	}

	

    return 0;

}


the data file is alphabet.dat and its just the alphabet in all caps.

#2
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Check these out:
C2j Converter - convert C-code sourse into Java classes

Convert C/C++ to Java? - JVM Languages | Google Groups
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#3
Nathandelane

Nathandelane

    Newbie

  • Members
  • PipPip
  • 22 posts
There are a couple of ways you could probably do this. Here are two that aren't very different:


import java.io.*;


public class Main {


	public static void main(String[] args) {

		char[] array = new char[30];

		int cnt;

		

		try {

			FileReader ifp = new FileReader("alphabet.dat");

			

			if(ifp != null) {

				for(cnt = 0; cnt < 26; cnt +=1) {

					array[cnt] = (char)ifp.read();

					System.out.format("%c", array[cnt]);

				}

				

				System.out.print(System.getProperty("line.separator"));

				

				for(cnt = 0; cnt < 10; cnt +=1) {

					System.out.format("%c", array[cnt]);

				}

				

				System.out.print(System.getProperty("line.separator"));

				

				for(cnt = 20; cnt < 26; cnt += 1) {

					System.out.format("%c", array[cnt]);

				}

				

				System.out.print(System.getProperty("line.separator"));

								

				System.out.format("%c%s", array[10], System.getProperty("line.separator"));

				

				for(cnt = 26; cnt > -1; cnt -= 1) {

					System.out.format("%c", array[cnt]);

				}

				

				System.out.print(System.getProperty("line.separator"));

			}

		} catch (FileNotFoundException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();

		}

	}


}


Here is the second one; just slightly different:

import java.io.*;


public class Main {


	public static void main(String[] args) {

		char[] array = new char[30];

		int cnt;

		

		try {

			FileReader ifp = new FileReader("alphabet.dat");

			

			if(ifp != null) {

				if(ifp.read(array, 0, 26) > 0) {

					for(cnt = 0; cnt < 26; cnt +=1) {

						System.out.format("%c", array[cnt]);

					}

					

					System.out.print(System.getProperty("line.separator"));

					

					for(cnt = 0; cnt < 10; cnt +=1) {

						System.out.format("%c", array[cnt]);

					}

					

					System.out.print(System.getProperty("line.separator"));

					

					for(cnt = 20; cnt < 26; cnt += 1) {

						System.out.format("%c", array[cnt]);

					}

					

					System.out.print(System.getProperty("line.separator"));

									

					System.out.format("%c%s", array[10], System.getProperty("line.separator"));

					

					for(cnt = 26; cnt > -1; cnt -= 1) {

						System.out.format("%c", array[cnt]);

					}

					

					System.out.print(System.getProperty("line.separator"));

				}

			}

		} catch (FileNotFoundException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();

		}

	}


}


They both output:


abcdefghijklmnopqrstuvwxyz

abcdefghij

uvwxyz

k

 zyxwvutsrqponmlkjihgfedcba


when alphabet.dat is in the same directory as the class file.