Skip to main content
Commonmark migration
Source Link

#Java 7, 159 158 149 154 bytes

Java 7, 159 158 149 154 bytes

String c(String[]a){a[0]=a[0].split("\\d")[0]+"] = {\b";for(String i:a)a[0]+=i.split("= [{]*")[1];return a[0].replace(";",", ").replaceFirst("..$","};");} 

Multiple bytes saved thanks to @cliffroot.

Ungolfed & test code:

Try it here.

class M{ static String c(String[] a){ a[0] = a[0].split("\\d")[0] + "] = {\b"; for(String i : a){ a[0] += i.split("= [{]*")[1]; } return a[0].replace(";", ", ").replaceFirst("..$", "};"); } public static void main(String[] a){ System.out.println(c(new String[]{ "spam eggs[10];", "eggs[0] = 0;", "eggs[1] = 4;", "eggs[2] = 8;", "eggs[3] = -3;", "eggs[4] = 3;", "eggs[5] = 7;", "eggs[6] = 888;", "eggs[7] = 555;", "eggs[8] = 0;", "eggs[9] = -2;" })); System.out.println(c(new String[]{ "char ans[2]", "ans[0] = 52;", "ans[1] = 50;" })); System.out.println(c(new String[]{ "blah_blah quux[1];", "quux[0] = 105;" })); } } 

Output:

spam eggs[] = {0, 4, 8, -3, 3, 7, 888, 555, 0, -2}; char ans[] = {52, 50}; blah_blah quux[] = {105}; 

#Java 7, 159 158 149 154 bytes

String c(String[]a){a[0]=a[0].split("\\d")[0]+"] = {\b";for(String i:a)a[0]+=i.split("= [{]*")[1];return a[0].replace(";",", ").replaceFirst("..$","};");} 

Multiple bytes saved thanks to @cliffroot.

Ungolfed & test code:

Try it here.

class M{ static String c(String[] a){ a[0] = a[0].split("\\d")[0] + "] = {\b"; for(String i : a){ a[0] += i.split("= [{]*")[1]; } return a[0].replace(";", ", ").replaceFirst("..$", "};"); } public static void main(String[] a){ System.out.println(c(new String[]{ "spam eggs[10];", "eggs[0] = 0;", "eggs[1] = 4;", "eggs[2] = 8;", "eggs[3] = -3;", "eggs[4] = 3;", "eggs[5] = 7;", "eggs[6] = 888;", "eggs[7] = 555;", "eggs[8] = 0;", "eggs[9] = -2;" })); System.out.println(c(new String[]{ "char ans[2]", "ans[0] = 52;", "ans[1] = 50;" })); System.out.println(c(new String[]{ "blah_blah quux[1];", "quux[0] = 105;" })); } } 

Output:

spam eggs[] = {0, 4, 8, -3, 3, 7, 888, 555, 0, -2}; char ans[] = {52, 50}; blah_blah quux[] = {105}; 

Java 7, 159 158 149 154 bytes

String c(String[]a){a[0]=a[0].split("\\d")[0]+"] = {\b";for(String i:a)a[0]+=i.split("= [{]*")[1];return a[0].replace(";",", ").replaceFirst("..$","};");} 

Multiple bytes saved thanks to @cliffroot.

Ungolfed & test code:

Try it here.

class M{ static String c(String[] a){ a[0] = a[0].split("\\d")[0] + "] = {\b"; for(String i : a){ a[0] += i.split("= [{]*")[1]; } return a[0].replace(";", ", ").replaceFirst("..$", "};"); } public static void main(String[] a){ System.out.println(c(new String[]{ "spam eggs[10];", "eggs[0] = 0;", "eggs[1] = 4;", "eggs[2] = 8;", "eggs[3] = -3;", "eggs[4] = 3;", "eggs[5] = 7;", "eggs[6] = 888;", "eggs[7] = 555;", "eggs[8] = 0;", "eggs[9] = -2;" })); System.out.println(c(new String[]{ "char ans[2]", "ans[0] = 52;", "ans[1] = 50;" })); System.out.println(c(new String[]{ "blah_blah quux[1];", "quux[0] = 105;" })); } } 

Output:

spam eggs[] = {0, 4, 8, -3, 3, 7, 888, 555, 0, -2}; char ans[] = {52, 50}; blah_blah quux[] = {105}; 
Fixed code after misinterpreted the question (spaces are necessary in the output and not optional)
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 155
  • 394

#Java 7, 159 158 149149 154 bytes

String c(String[]a){a[0]=a[0].split("\\d")[0]+"]=[0]+"] = {\b";for(String i:a)a[0]+=i.split("=["= [{]*")[1];return a[0].replace(';'";",'",' ").replaceFirst("..$","};");} 

Saved 1 byte in exchange for less pretty output; 9 Multiple bytes saved thanks to @cliffroot.

Ungolfed & test code:

Try it here.

class M{ static String c(String[] a){ a[0] = a[0].split("\\d")[0] + "]="] = {\b"; for(String i : a){ a[0] += i.split("=["= [{]*")[1]; } return a[0].replace(';'";", '",' ").replaceFirst("..$", "};"); } public static void main(String[] a){ System.out.println(c(new String[]{ "spam eggs[10];", "eggs[0] = 0;", "eggs[1] = 4;", "eggs[2] = 8;", "eggs[3] = -3;", "eggs[4] = 3;", "eggs[5] = 7;", "eggs[6] = 888;", "eggs[7] = 555;", "eggs[8] = 0;", "eggs[9] = -2;" })); System.out.println(c(new String[]{ "char ans[2]", "ans[0] = 52;", "ans[1] = 50;" })); System.out.println(c(new String[]{ "blah_blah quux[1];", "quux[0] = 105;" })); } } 

Output:

spam eggs[]=eggs[] = {0, 4, 8, -3, 3, 7, 888, 555, 0, -2}; char ans[]=ans[] = {52, 50}; blah_blah quux[]=quux[] = {105}; 

#Java 7, 159 158 149 bytes

String c(String[]a){a[0]=a[0].split("\\d")[0]+"]={\b";for(String i:a)a[0]+=i.split("=[{]*")[1];return a[0].replace(';',',').replaceFirst(".$","};");} 

Saved 1 byte in exchange for less pretty output; 9 bytes saved thanks to @cliffroot.

Ungolfed & test code:

Try it here.

class M{ static String c(String[] a){ a[0] = a[0].split("\\d")[0] + "]={\b"; for(String i : a){ a[0] += i.split("=[{]*")[1]; } return a[0].replace(';', ',').replaceFirst(".$", "};"); } public static void main(String[] a){ System.out.println(c(new String[]{ "spam eggs[10];", "eggs[0] = 0;", "eggs[1] = 4;", "eggs[2] = 8;", "eggs[3] = -3;", "eggs[4] = 3;", "eggs[5] = 7;", "eggs[6] = 888;", "eggs[7] = 555;", "eggs[8] = 0;", "eggs[9] = -2;" })); System.out.println(c(new String[]{ "char ans[2]", "ans[0] = 52;", "ans[1] = 50;" })); System.out.println(c(new String[]{ "blah_blah quux[1];", "quux[0] = 105;" })); } } 

Output:

spam eggs[]={0,4,8,-3,3,7,888,555,0,-2}; char ans[]={52,50}; blah_blah quux[]={105}; 

#Java 7, 159 158 149 154 bytes

String c(String[]a){a[0]=a[0].split("\\d")[0]+"] = {\b";for(String i:a)a[0]+=i.split("= [{]*")[1];return a[0].replace(";",", ").replaceFirst("..$","};");} 

Multiple bytes saved thanks to @cliffroot.

Ungolfed & test code:

Try it here.

class M{ static String c(String[] a){ a[0] = a[0].split("\\d")[0] + "] = {\b"; for(String i : a){ a[0] += i.split("= [{]*")[1]; } return a[0].replace(";", ", ").replaceFirst("..$", "};"); } public static void main(String[] a){ System.out.println(c(new String[]{ "spam eggs[10];", "eggs[0] = 0;", "eggs[1] = 4;", "eggs[2] = 8;", "eggs[3] = -3;", "eggs[4] = 3;", "eggs[5] = 7;", "eggs[6] = 888;", "eggs[7] = 555;", "eggs[8] = 0;", "eggs[9] = -2;" })); System.out.println(c(new String[]{ "char ans[2]", "ans[0] = 52;", "ans[1] = 50;" })); System.out.println(c(new String[]{ "blah_blah quux[1];", "quux[0] = 105;" })); } } 

Output:

spam eggs[] = {0, 4, 8, -3, 3, 7, 888, 555, 0, -2}; char ans[] = {52, 50}; blah_blah quux[] = {105}; 
Output is pretty now, although without spaces
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 155
  • 394

#Java 7, 159 158 149 bytes

String c(String[]a){a[0]=a[0].split("\\d")[0]+"]={\b";for(String i:a)a[0]+=i.split("=[{]*")[1];return a[0].replace(';',',').replaceFirst(".$","};");} 

Saved 1 byte in exchange for less pretty output;Saved 1 byte in exchange for less pretty output; 9 bytes saved thanks to @cliffroot.

Ungolfed & test code:

Try it here.

class M{ static String c(String[] a){ a[0] = a[0].split("\\d")[0] + "]={\b"; for(String i : a){ a[0] += i.split("=[{]*")[1]; } return a[0].replace(';', ',').replaceFirst(".$", "};"); } public static void main(String[] a){ System.out.println(c(new String[]{ "spam eggs[10];", "eggs[0] = 0;", "eggs[1] = 4;", "eggs[2] = 8;", "eggs[3] = -3;", "eggs[4] = 3;", "eggs[5] = 7;", "eggs[6] = 888;", "eggs[7] = 555;", "eggs[8] = 0;", "eggs[9] = -2;" })); System.out.println(c(new String[]{ "char ans[2]", "ans[0] = 52;", "ans[1] = 50;" })); System.out.println(c(new String[]{ "blah_blah quux[1];", "quux[0] = 105;" })); } } 

Output:

spam eggs[]={0,4,8,-3,3,7,888,555,0,-2}; char ans[]={52,50}; blah_blah quux[]={105}; 

#Java 7, 159 158 149 bytes

String c(String[]a){a[0]=a[0].split("\\d")[0]+"]={\b";for(String i:a)a[0]+=i.split("=[{]*")[1];return a[0].replace(';',',').replaceFirst(".$","};");} 

Saved 1 byte in exchange for less pretty output; 9 bytes saved thanks to @cliffroot.

Ungolfed & test code:

Try it here.

class M{ static String c(String[] a){ a[0] = a[0].split("\\d")[0] + "]={\b"; for(String i : a){ a[0] += i.split("=[{]*")[1]; } return a[0].replace(';', ',').replaceFirst(".$", "};"); } public static void main(String[] a){ System.out.println(c(new String[]{ "spam eggs[10];", "eggs[0] = 0;", "eggs[1] = 4;", "eggs[2] = 8;", "eggs[3] = -3;", "eggs[4] = 3;", "eggs[5] = 7;", "eggs[6] = 888;", "eggs[7] = 555;", "eggs[8] = 0;", "eggs[9] = -2;" })); System.out.println(c(new String[]{ "char ans[2]", "ans[0] = 52;", "ans[1] = 50;" })); System.out.println(c(new String[]{ "blah_blah quux[1];", "quux[0] = 105;" })); } } 

Output:

spam eggs[]={0,4,8,-3,3,7,888,555,0,-2}; char ans[]={52,50}; blah_blah quux[]={105}; 

#Java 7, 159 158 149 bytes

String c(String[]a){a[0]=a[0].split("\\d")[0]+"]={\b";for(String i:a)a[0]+=i.split("=[{]*")[1];return a[0].replace(';',',').replaceFirst(".$","};");} 

Saved 1 byte in exchange for less pretty output; 9 bytes saved thanks to @cliffroot.

Ungolfed & test code:

Try it here.

class M{ static String c(String[] a){ a[0] = a[0].split("\\d")[0] + "]={\b"; for(String i : a){ a[0] += i.split("=[{]*")[1]; } return a[0].replace(';', ',').replaceFirst(".$", "};"); } public static void main(String[] a){ System.out.println(c(new String[]{ "spam eggs[10];", "eggs[0] = 0;", "eggs[1] = 4;", "eggs[2] = 8;", "eggs[3] = -3;", "eggs[4] = 3;", "eggs[5] = 7;", "eggs[6] = 888;", "eggs[7] = 555;", "eggs[8] = 0;", "eggs[9] = -2;" })); System.out.println(c(new String[]{ "char ans[2]", "ans[0] = 52;", "ans[1] = 50;" })); System.out.println(c(new String[]{ "blah_blah quux[1];", "quux[0] = 105;" })); } } 

Output:

spam eggs[]={0,4,8,-3,3,7,888,555,0,-2}; char ans[]={52,50}; blah_blah quux[]={105}; 
9 bytes saved thanks to @cliffroot
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 155
  • 394
Loading
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 155
  • 394
Loading