Question

I have problem with this task:

User will type some text.

  1. If text has 'a' triple this letter.
  2. If text has simply 'd' delete this letter.
  3. If text has double 'b' write only one 'b'.

I understand this outlines but I do not know how to do it. How should I search text with charAt? What if I find where it is, where should I write this trippled letter?

Était-ce utile?

La solution

It really is not that difficult! Go about it sequentially. First replace all the "a"s as you want them to be replaced, then all the "d"s and then all the "b"s. Here's a simple example with replace():

public static void main(String args[]) {
    System.out.print("Word:");
    Scanner scanner = new Scanner(System.in);
    String foo = scanner.next();
    foo = foo.replace("a", "aaa");
    System.out.println(foo);
    foo = foo.replace("d", "");
    System.out.println(foo);
    foo = foo.replace("bb", "b");
    System.out.println(foo);
}

Let me know if this is what you wanted. Also, you could do this with charAt() and String manipulation as well, but that would be a bit more involved.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top