Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

doubleChar java challenge

Hi,
I am working on below challenge
http://codingbat.com/prob/p165312
I tried my code as below
public String doubleChar(String str) {

  
  for(int i=0;i<str.length();i++){
  char[] arrays=new char[]{str.substring(i, i+1);
  }
  //once get arrays get each character and add one more and make it string
  
}

Open in new window


how to  improve my approach, results and design of this challenge. How do i

make a graphical venn or flow chart or some other relevant diagram to design it

before writing code to decide best strategy?
 Please advise
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Curiosity overwhelms me ... what is this homework for?   Most technical questions are not phrased as a 'challenge' to 'improve my approach, results and design'.
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

public String doubleChar(String str) {

StringBuilder sb=new StringBuilder();

String strNew=null;
int j=0;
for(int i=0;i<=str.length()-1;i++)
{
strNew=str.substring(j,j+1);
sb.append(strNew+strNew);
j++;
}
return sb.toString();
  
}

Open in new window


above passed all tests
Avatar of gudii9

ASKER

 return str.replaceAll("(.)","$1$1");

what is meaning of above.

please advise
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

for (char c : str.toCharArray()) {

is this new function from java 1.6 or later?
Avatar of dpearson
dpearson

Nope it's not new - I think toCharArray() has been there since Java came along:
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#toCharArray()

Doug