0

So the goal is to look for patterns like "zip" and "zap" in the string, starting with 'z' and ending with 'p'. Then, for all such strings, delete the middle letter.

What I had in mind was that I use a for loop to check each letter of the string and once it reaches a 'z', it gets the indexOf('p') and puts that and everything in the middle into an ArrayList, while deleting itself from the original string so that indexOf('p') can be found.

How can I do that?

This is my code so far:

package Homework;

import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;

public class ZipZap {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        List < String > list = new ArrayList < String > ();

        System.out.print("Write a sentence with no spaces:");
        String sen = in .next();
        int len = sen.length();
        int p1 = sen.indexOf('p');
        String word = null;
        String idk = null;

        for (int i = 0; i < len; i++) {
            if (sen.charAt(i) == 'z') {
                word = sen.substring(i, p1 + 1);
                list.add(word);
                idk = sen.replace(word, "");
                i = 0;
            }
        }
    }

}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • Perhaps have a look at `String#split` to split the `String` into an array of words – MadProgrammer Feb 11 '17 at 05:39
  • what about using String.split to find all the "z" (http://stackoverflow.com/questions/2206378/how-to-split-a-string-but-also-keep-the-delimiters). then for each word in the list, replace all letters from the second position to before the first "p" – Angel Koh Feb 11 '17 at 05:39
  • This code will fail if a 'p' occurs before a letter 'z'. I.e., it fails for "xxpxxzxxpxx". It also fails for "xxzxxpxxz". That's because you are only calling sen.indexOf('p') once. You should be calling it after you find a 'z', and you should be using the version of indexOf() that takes two arguments so you can specify the 'z' position as the start position for the search. – Richard Schwartz Feb 11 '17 at 05:47
  • I know this sounds kind of dumb, but Im not sure how to use the two argument method. Can you give a brief explanation maybe? –  Feb 11 '17 at 05:53

2 Answers2

1

use this , here i am using "\bz.p\b" pattern for finding any word that contains starting char with z and end with p anything can be in between

 String s ="Write a sentence with no zip and zap spaces:";
 s=s.replaceAll("\\bz.p\\b", "zp");
 System.out.println(s);

output:

Write a sentence with no zp and zp spaces:

or it can be

 s.replaceAll("z\\w+p", "zp");

here you can check you string

https://regex101.com/r/aKaNTJ/2

jitendra varshney
  • 3,484
  • 1
  • 21
  • 31
0

I think you’re saying that input zipzapityzoop, for example, should be changed to zpzpityzp with i, a and oo going into list. Please correct me if I misunderstood your intention.

You are on the way and seem to understand the basics. The issues I see are minor, but of course you want to fix them:

  • As @RicharsSchwartz mentions, to find all strings like zip, zap and zoop, you need to find p after every z you find. When you have found z at index i, you may use sen.indexOf('p', i + 1) to find a p after the z (the second argument causes the search to begin at that index).
  • Every time you have found a z, you are setting i back to 0, this starting over from the beginning of the string. No need to do that, and this way your program will never stop.
  • sen.substring(i, p1+1) takes out all of zip when I understood you only wanted i. You need to adjust the arguments to substring().
  • Your use of sen.replace(word, "") will replace all occurences of word. So once you fix your program to take out a from zap, zappa will become zpp (not zppa), and azap will be zp. There is no easy way to remove just one specific occurrence of a substring from a String. I think the solution is to use the StringBuilder class. It has a delete method that will remove the part between two specified indices, which is what you need.
  • Finally you are assigning the changed string to a different variable idk, but then you continue to search sen. This is like assigning zpzapityzoop, zipzpityzoop and zipzapityzp to idk in turn, but never zpzpityzp. However, if you use a StringBuilder as I just suggested, just use the same StringBuilder all the way through and you will be fine.
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161