C – Java – 019: Please convert the following C code to Java so that the Java program executes successfully passing the test cases.
#include <stdio.h> #include <stdlib.h> int main() { int N, index = 0; scanf("%d", &N); char str[1001]; for(int ctr = 1; ctr <= N; ctr++) { index += scanf("n%s", &str[index]); printf("%sn", str); } return 0; }
Converted Java Code
import java.util.*; public class Hello{ public static void main(String []args){ Scanner sc= new Scanner(System.in); int N=sc.nextInt(); String S=""; for (int ctr=0;ctr<=N; ctr++){ S+=sc.nextLine(); System.out.println(S); S=S.substring(0,ctr); } } }