0

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 ");
      }

     }   


}

}

user2872194
  • 147
  • 2
  • 12

6 Answers6

4

Alternative to this will be something like this

for(int i=0;i<input.length();i++){
if(input.charAt(i)=='z'||input.charAt(i)=='Z')
system.out.println(i + " z");
}
Dimitri
  • 453
  • 3
  • 11
2

Don't use substrings. Use String.toCharArray() and check in place through each iteration. Substrings will create new strings for every iteration. That's wasteful.

mttdbrd
  • 1,791
  • 12
  • 17
1

If you reset the counter every time you encounter a 'z' you don't need the array at all, just go through the input string, increment if the character is not a z, reset otherwise. Maintain a list of the counters by appending the current counter to the list every time you reset it.

Also use charAt() to get the characters in the string.

Giovanni Botta
  • 9,626
  • 5
  • 51
  • 94
0

How about the String.indexOf method?

it returns the index of the first occurrence of a character, but you can set a start point. this will let you avoid using substrings.

that said, this really shouldn't take much memory, so unless you actually have a memory issue, or this is an assignment for a class or the like, it is fine to keep what you have.

lkrasner
  • 112
  • 2
  • 13
0

I will say this just because you specifically ask for an answer that uses an array. In reality it would be much better to do it the way people tell you to in the current answers.

If your string is really big, you are going to create a huge array of int of the same size which is wasteful. So the best thing to do is to first define a much smaller array, say 1/10th of the size of the string array and put you results in it. Keep a counter that tells you how many 'z' you have seen so that you put the number of characters before the 3rd 'z' at index 2 and so on.

Now, if the size of the array is not big enough (if the count of z reaches the size of the array), you will have to reallocate some space for a bigger array and copy your results in it. See resize an array. This way you use less memory.

At the end, go through your array with a for loop and print the results.

Community
  • 1
  • 1
Victor D.
  • 149
  • 2
  • 9
0

similar to @user3058333 answer, but needs two counters to print actual numbers. I would have commented in his/her answer, but I dont have enough reputation for comment. The below actually gives the o/p you mentioned.

    for(int i=0,j=0;i<input.length();i++,j++){
        if(input.charAt(i)=='z'||input.charAt(i)=='Z'){
            System.out.println(j + " z");
            j= -1;
            }
        }
    }
Ayan
  • 515
  • 4
  • 9
  • 20