Splitting strings by characters

edited January 2017 in Questions about Code

I want to write a function which take strings as input and returns an array of characters consisting of the characters in the order they originally appear in the string. For example, explode( "Dirk" ) should return the array {'D', 'i', 'r', 'k'}. However, I cannot get it to work.

char[] explode(String s) { String theString = s; char[] c = new char[theString.length()]; for (int i = 0; i < theString.length(); i++) { c[i] = theString.charAt(i); return c[i]; } }

I also want to write a function that behaves like the above one in reverse. Taking an array of characters as input and returns a String consisting of all the characters in the array glued together. For example, returning ("Dirk") after inputting the array {'D', 'i', 'r', 'k'}.

Tagged:

Answers

Sign In or Register to comment.