Hey all I have already completed this problem, but I was wondering if there was a way I could do it without using unnecessary amounts of memory, while still using an Array. Below is the problem and my code.
Question:Implement a method named preZee that will accept a string parameter named book. It will output to the screen the number of characters before each ‘z’.
Example: if the string is: “my house is ez to find. Go to z street and turn left. It is ze house with the black starz on the 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 a 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 ");
}
}
}
}