java - Another way without abusing memory -
hey have completed problem, wondering if there way without using unnecessary amounts of memory, while still using array. below problem , code.
question:implement method named prezee accept string parameter named book. output screen number of characters before each ‘z’.
example: if string is: “my house ez find. go z street , turn left. ze house black starz on door.”
the output should read: 13 z, 16 z, 29 z, 27 z
code:
import java.util.*; public class prezee{ public static void main(string[] args){ scanner user=new scanner(system.in); system.out.println("please enter sentence"); string input=user.nextline(); int[] anarray; anarray=new int[input.length()]; int count=0; for(int i=0;i<input.length();i++){ string subst=input.substring(i,i+1); if(!(subst.equalsignorecase("z"))){ count ++; } else{ anarray[i]=count; count=0; } } for(int j=0;j<input.length();j++){ if(anarray[j]!=0){ system.out.print(anarray[j]+ "z "); } } }
}
alternative this
for(int i=0;i<input.length();i++){ if(input.charat(i)=='z'||input.charat(i)=='z') system.out.println(i + " z"); }
Comments
Post a Comment