0
Splitting strings Java code coach
Only 2 hidden test cases fail and i donât know why. Hereâs my code: import java.util.*; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); String s = input.nextLine(); int n = input.nextInt(); ArrayList<Character> r = new ArrayList<Character>(); String res = ""; for(int i=0;i<s.length();i++){ if(i==n){ n=n+n; r.add('-'); } r.add(s.charAt(i)); } for(char j : r){ res += j; } System.out.println(res); } }
2 RĂ©ponses
+ 4
Nikola Markov , your code could be rewritten this way. Add the symbol "-", only if position in string is multiple of n and differ from 0. Look at the code. Hope it helps you.
https://code.sololearn.com/c2R13bRmJ47R/?ref=app
+ 1
Thank you for the help!